当前位置: 首页 > news >正文

【Java数据结构】——常见力扣题综合

n数之和

1.两数之和

    static public int[] twoSum(int[] numbers, int target) {int i = 0;int j = numbers.length - 1;while (i < j) {int sum = numbers[i] + numbers[j];if (sum < target) {i++;} else if (sum > target) {j--;} else {break;}}return new int[]{i + 1, j + 1};}

2.三数/四数之和

    public List<List<Integer>> threeSum1(int[] nums) {List<List<Integer>> result = new ArrayList<>();Arrays.sort(nums);//定义指针int l = 0;int r = nums.length - 1;dfs(nums, result, new LinkedList<>(), 3, l, r, 0);return result;}public void dfs(int[] nums, List<List<Integer>> result, LinkedList<Integer> stack, int n, int l, int r, int target){if (n == 2){twoSum(nums, result, stack, l , r, target);return;}for (int i = l; i < r - (n - 2); i++) {// 检查重复if (i > l && nums[i] == nums[i - 1]){continue;}stack.push(nums[i]);dfs(nums, result, stack, n - 1, i + 1, r, target - nums[i]);stack.pop();}}public List<List<Integer>> twoSum(int[] nums, List<List<Integer>> result, LinkedList<Integer> stack, int l, int r, int target) {while(l < r){int sum = nums[l] + nums[r];if (sum == target){List<Integer> list = new ArrayList<>(stack);list.add(nums[l]);list.add(nums[r]);result.add(list);//去重while(l < r && nums[l] == nums[l + 1]){l++;}while(l < r && nums[r] == nums[r - 1]){r--;}l++;r--;}else if (sum < target){l++;}else {r--;}}return result;}

盛水最多的容器(双指针)

    static int maxArea(int[] height) {int i = 0;int j = height.length - 1;int max = 0;while (i < j) {if (height[i] < height[j]) {int area = (j - i) * height[i];max = Math.max(max, area);i++;} else {int area = (j - i) * height[j];max = Math.max(max, area);j--;}}return max;}

滑动窗口最大值(单调队列)

单调队列:要保证值从大到小排列,如果加入队列时前面的元素比后面的大,就要先把比它大的元素先移出来再入队。

构造一个单调递减的队列出来

滑动窗口最大值

    static int[] maxSlidingWindow(int[] nums, int k) {MonotonicQueue queue = new MonotonicQueue();List<Integer> list = new ArrayList<>();for (int i = 0; i < nums.length; i++) {// 检查队列头部元素,超过滑动窗口范围要移除if (i >= k && queue.peek() == nums[i - k]) {queue.poll();}int num = nums[i];queue.offer(num);if (i >= (k - 1)) {
//                System.out.println(queue.peek());list.add(queue.peek());}}return list.stream().mapToInt(Integer::intValue).toArray();}

接雨水(单调栈)

    static int trap(int[] heights) {LinkedList<Data> stack = new LinkedList<>();int sum = 0;for (int i = 0; i < heights.length; i++) {Data right = new Data(heights[i], i);while (!stack.isEmpty()&& stack.peek().height < right.height) {Data pop = stack.pop();Data left = stack.peek();if (left != null) { // 计算水的容量int width = right.i - left.i - 1;int height = Math.min(left.height, right.height) - pop.height;sum += width * height;}}stack.push(right);
//            System.out.println(stack);}return sum;}

字符串匹配

最长公共前缀

    static String longestCommonPrefix(String[] strings) {/*情况1:比较某一列时,遇到不同字符,i 之前的字符就是解情况2:比较某一列时,遇到字符串长度不够,i 之前的字符就是解情况3:i 循环自然结束,此时第一个字符串就是解*/char[] first = strings[0].toCharArray(); // 第一个字符串for (int i = 0; i < first.length; i++) {char ch = first[i];for (int j = 1; j < strings.length; j++) {/*if (strings[j].length() == i) { // 情况 2return new String(first, 0, i);}if(ch != strings[j].charAt(i)) { // 情况 1return new String(first, 0, i);} */if (strings[j].length() == i || ch != strings[j].charAt(i)) {return new String(first, 0, i);}}}return strings[0];}

最长回文子串

    static String longestPalindrome(String s) {left = 0;right = 0;char[] chars = s.toCharArray();for (int i = 0; i < chars.length - 1; i++) {extend(chars, i, i); // 一个字符作为中心点extend(chars, i, i + 1); // 两个字符作为中心点}return new String(chars, left, right - left + 1);}static int left; // istatic int right; // jstatic void extend(char[] chars, int i, int j) {while (i >= 0 && j < chars.length&& chars[i] == chars[j]) { // 如果是回文,扩大回文范围i--; // -1j++; // 4}// 退出循环时,i和j指向的不是回文,需要还原i++;j--;if (j - i > right - left) {left = i;right = j;}}

最长覆盖子串

public class MinWindowLeetcode76_me {static class Result {int i;int j;public Result(int i, int j) {this.i = i;this.j = j;}}static String minWindow(String s, String t) {//目标t数组char[] target = t.toCharArray();//统计目标数组出现次数int[] targetCount = new int[128];for (char ch : target) {targetCount[ch]++;}print(targetCount);
//---------------目标数组统计-------------------------//原数组schar[] source = s.toCharArray();//统计原数组出现的次数int[] sourceCount = new int[128];//---------------原数组统计-------------------------int i = 0;int j = 0;int passCount = 0;for (int count : targetCount){if (count > 0){passCount++;}}int passed = 0;Result res = null;while (j < source.length) {char right = source[j];sourceCount[right]++;if (sourceCount[right] == targetCount[right]) {passed++;}while (passed == passCount && i <= j){if (res == null) {res = new Result(i, j);} else {if (j - i < res.j - res.i) {res = new Result(i, j);}}char left = source[i];sourceCount[left]--;if (sourceCount[left] < targetCount[left]) {passed--;}i++;}j++;}
//        System.out.println(res.i + " " + res.j);return res == null ? "" : new String(source, res.i, res.j - res.i + 1);}static void print(int[] count) {System.out.println(IntStream.range(0, count.length).filter(i -> count[i] != 0).boxed().collect(toMap(i -> (char) i.intValue(), i -> count[i])));}public static void main(String[] args) {System.out.println(minWindow("ADOBECODEBANC", "ABC")); // BANC}
}

设计短网址

设计推特

股票系列问题

http://www.dtcms.com/a/528797.html

相关文章:

  • 网站长期建设运营计划书江门营销网站建设
  • ProcDump 学习笔记(6.7):监视异常(未处理/首机会/消息过滤/进程终止)
  • C++编程实践——Linux下的CPU控制
  • NTRU 公钥加密系统详解
  • 深入浅出 VGGNet:经典卷积神经网络解析
  • 盐城整站优化柳州做网站去哪家公司好
  • 协程:实战与系统集成(高级篇)
  • 芯片验证基石UVM:高效验证的方法论与挑战
  • 旅游网站开发的作用seo快排技术教程
  • 3DS-GBA-GBC-NDS-switch梦可宝精灵游戏合集 -全汉化游戏
  • VCS Verdi 2023安装
  • R语言~T检验
  • 春季大扫除:清理 Arch Linux 中的垃圾
  • 未在props中声明的属性
  • php网站iis设置同心食品厂网站建设项目任务分解
  • 中国启用WPS格式进行国际交流:政策分析与影响评估
  • 中文域名做的网站有哪些网站域名怎么做分录
  • Docker使用【镜像】
  • 全链路智能运维中的业务连续性保障与容灾切换机制
  • linux的文件系统
  • 英语四级真题完整版(1990-2025)|2025年6月最新试题+答案解析|可打印PDF
  • 网站开发 项目的人员分配建筑工程网络计划编制软件
  • React 06
  • 红河县网站建设昆明网站建设哪家
  • 社区互助养老系统设计与实现方案
  • 服装购物商城网站建设安徽六安旅游必去十大景点
  • 「用Python来学微积分」14. 连续函数的运算与初等函数的连续性
  • 红酒商城网站建设广告设计案例网站
  • Linux内核进程管理子系统有什么第六十七回 —— 进程主结构详解(63)
  • 哪个网站可以接针织衫做单淘宝上找人做网站