了解了一下尿毒症,每周透析,肾不行,是一辈子的负担,心态会病;要保护好健康的身体
今天新闻中的尿毒症患者伤医时间,其实背后隐患很大。
// 判断序列是否是二叉搜索树的后序遍历序列
// 同理可以判断二叉搜索树的前序遍历
// 中序直接判断是不是递增序列
1 static boolean IsSquenceOfBST(int [] arr,int l,int r){ 2 3 int root = arr[r]; 4 5 int i=0; 6 7 while(arr[i] < root){ 8 9 i++; 10 11 } 12 13 int j=i; 14 15 while(j<r){ 16 17 if(arr[j]< root) return false; 18 19 j++; 20 21 } 22 23 if(i>0 && !IsSquenceOfBST(arr,l,i-1)){ 24 25 return false; 26 27 } 28 29 30 31 if(j<r && !IsSquenceOfBST(arr,i,r-1) ){ 32 33 return false; 34 35 } 36 37 return true; 38 39 } 40 41 public static void main(String[] args) { 42 43 int [] arr = new int[]{5,1,7,4}; 44 45 int [] test1= new int[]{1,3,2,4}; 46 47 int [] test2= new int[]{5,7,6,4}; 48 49 int [] test3= new int[]{5,7,6,9,11,10,8}; 50 51 boolean b= IsSquenceOfBST(arr,0,arr.length-1); 52 53 }
Leetcode 39 dfs + 排序去重
* @param candidates 数组输入 * @param len 输入数组的长度,冗余变量 * @param residue 剩余数值 * @param begin 本轮搜索的起点下标 * @param path 从根结点到任意结点的路径 * @param res 结果集变量 */ private void dfs(int[] candidates, int len, int residue, int begin, Deque<Integer> path, List<List<Integer>> res) { if (residue == 0) { // 由于 path 全局只使用一份,到叶子结点的时候需要做一个拷贝 res.add(new ArrayList<>(path)); return; } for (int i = begin; i < len; i++) { // 在数组有序的前提下,剪枝 if (residue - candidates[i] < 0) { break; } path.addLast(candidates[i]); dfs(candidates, len, residue - candidates[i], i, path, res); path.removeLast(); } }
Leetcode 40
Dfs 去重,
public List<List<Integer>> combinationSum2(int[] candidates, int target) { List<List<Integer>> res = new ArrayList<>(); Arrays.sort(candidates); ArrayList<Integer> path = new ArrayList<>(); dfs(candidates,target,0,path,res); return res; } void dfs(int [] arr ,int remain,int begin,ArrayList<Integer> path,List<List<Integer>> res){ if(remain==0){ // if(!res.contains(new ArrayList(path))){ // res.add(new ArrayList(path)); // } res.add(new ArrayList(path)); return; } for( int i=begin;i<arr.length;i++){ if(remain < arr[i]) break; if(i>begin && arr[i]==arr[i-1]) continue; path.add(arr[i]); dfs(arr,remain-arr[i],i+1,path,res); path.remove(path.size()-1); } }
二叉搜索树 转化成双向链表,java版,c++版
Java版更好理解
数组中出现次数超过 n/2 的数字。
采用标记次数法,但是最后要验证下,结果是不是真的有超过次数一半
O(N),采用快排思想,找出第k个
Java 优先队列,求海量数据top k,如果不能一次性加入内存,可以分批求
List<Integer> list = new ArrayList<>(); if (k > input.length || k == 0) { return list; } Queue<Integer> queue = new PriorityQueue<>(); for (int num : input) { if (queue.size() < k) { queue.add(num); } else if (queue.peek() < num){ queue.poll(); queue.add(num); } } while (k-- > 0) { list.add(queue.poll()); } return list;
剑指offer -从1到n整数中,1出现的次数;递归分析问题
递归写法, 1- 21345= 1-1345 和 1346 -21345,这样就可以递归了
然后分数位,最高位,其他位,递归;
第一个只出现一次的字符,ascii 可以不用map,用数组,然后按照原字符串遍历,找到次数为1的。
数组中的逆序对, 尤其适合归并算法
这个题需要练到能10min快速写出来
原文:https://www.cnblogs.com/lpc2020/p/12548899.html