首页 > 其他 > 详细

第三篇-日常记录

时间:2020-03-22 22:38:02      阅读:59      评论:0      收藏:0      [点我收藏+]

了解了一下尿毒症,每周透析,肾不行,是一辈子的负担,心态会病;要保护好健康的身体

今天新闻中的尿毒症患者伤医时间,其实背后隐患很大。

// 判断序列是否是二叉搜索树的后序遍历序列

// 同理可以判断二叉搜索树的前序遍历

// 中序直接判断是不是递增序列

技术分享图片
 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 }
View Code

 

 

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();

        }

}
View Code

 

 

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);

        }

    }
View Code

 

二叉搜索树 转化成双向链表,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;
View Code

 

 

剑指offer -从1到n整数中,1出现的次数;递归分析问题

递归写法, 1- 21345=  1-1345 和 1346 -21345,这样就可以递归了

然后分数位,最高位,其他位,递归;

 

第一个只出现一次的字符,ascii 可以不用map,用数组,然后按照原字符串遍历,找到次数为1的。

 

数组中的逆序对, 尤其适合归并算法

这个题需要练到能10min快速写出来

 

第三篇-日常记录

原文:https://www.cnblogs.com/lpc2020/p/12548899.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!