![](https://img-blog.csdnimg.cn/20201014180756927.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
算法
撇不完
今天不走,明天要跑
展开
-
区间问题
区间合并一个区间可以表示为[start,end],按start排序 List<List<Integer>> list = List.of(List.of(4, 5), List.of(1, 4)); list = list.stream() .sorted(Comparator.comparingInt(s -> s.get(0))) .collect(Collectors.toList()); Lis原创 2020-05-13 09:42:39 · 172 阅读 · 0 评论 -
树-图-递归
消消乐,填色游戏,DFSint[][] floodFill(int[][] image, int sr, int sc, int newColor) { int origColor = image[sr][sc]; fill(image, sr, sc, origColor, newColor); return image;}void fill(int[][] image, int x, int y, int origColor, int原创 2020-05-13 09:42:17 · 183 阅读 · 0 评论 -
双指针
去除有序数组中的重复元素,快慢指针int removeDuplicates(int[] nums){ int n = nums.length; if(n == 0) return 0; int slow = 0; int fast = 1; while(fast < n){ if(nums[fast]==nums[slow]]{ slow++; nums[slow] = num[fast];原创 2020-05-12 02:40:49 · 125 阅读 · 0 评论 -
回文串和回文序列,回文链表相关问题
最长回文串,左右指针中心扩展String longestStr(String s){ char[] chars = new char[s.length()]; String str = null; for(int i=0;i<s.length(),i++){ chars[i] = s.charAt[i]; } for(int i = 1;i<chars.length;i++){ str1=pilidrome(s,i,原创 2020-05-12 02:40:37 · 136 阅读 · 0 评论 -
水塘抽样算法
给一个未知长度的序列,如何在其中随机地选择k个元素?第i个元素被选择的概率是1/i,第i+1次不被替换的概率是1 - 1/(i+1),以此类推,相乘就是第i个元素最终被选中的概率,就是1/n。/* 返回链表中一个随机节点的值 */int getRandom(ListNode head) { Random r = new Random(); int i = 0, res = 0; ListNode p = head; // while 循环遍历...原创 2020-05-12 02:40:24 · 148 阅读 · 0 评论 -
union-Find
class UF{ int count; int[] parent; int[] size; public UF(int n){ this.count = n; parent = size = new int[n]; for(int i=0; i<n; i++){ parent[i] = i; size[i] = 1; } } publi.原创 2020-05-12 02:40:13 · 93 阅读 · 0 评论 -
二分查找判断子序列
寻找左侧边界int left_bound(int[] nums, int target){ int left = 0; int right = nums.length; while(left < right){ int mid = left + (right-left) / 2; if(nums[mid] = target){ right = mid; }else if(nums[mid] <原创 2020-05-12 02:39:58 · 166 阅读 · 0 评论 -
栈——判断括号合法性
boolean isValid(String str){ Stack stack = new Stack(); char[] strChar = new char[str.length()]; for (int i = 0; i < strChar.length; i++) { char c = str.charAt(i); if (c == '(' || c == '{' || c == '[') stack.push(c); else.原创 2020-05-12 02:39:45 · 260 阅读 · 0 评论