zoukankan      html  css  js  c++  java
  • 一些刷题时总结的重要知识点


     =========数组========

    1.如果题目要求O(n)复杂度,一般就需要考虑使用two pointer 或者sliding window或者HashMap、 HashSet

    eg: https://leetcode.com/problems/longest-consecutive-sequence/description/ 就是利用HashSet ,巧妙将有序的数组转化为无序,就可以不用考虑顺序,直接看有没有每个数相邻的数就行:对每个数,以该数为中心,向左右扩张,直到不连续为止,记下最大长度。

    public class LongestConsecutiveSequnce {
    
        public int longestConsecutive(int[] nums) {
            if (nums.length == 0) return 0;
            HashSet<Integer> set = new HashSet<>();
            for (int num : nums) set.add(num);
            int res = 0;
            for (int num : nums) {
                int tmpLongest = 1;
                for (int i = num + 1; set.contains(i); i++) {
                    set.remove(i);
                    tmpLongest ++;
                }
                for (int i = num - 1; set.contains(i); i--) {
                    set.remove(i);
                    tmpLongest ++;
                }
                res = Math.max(res, tmpLongest);
            }
            return res;
        }
    }
    View Code

    =========树========

    1. 删除树节点:代替该节点的是该节点的右子树(如果存在左、右子树的话)的最小值(右子树的最左端),然后就变为删除最小值的节点,可变为递归解决。

    参考题目:https://leetcode.com/problems/delete-node-in-a-bst/description/

    =========排序========  

    快速排序的变种:

    1.-------快速选择算法-------

    应用:用于在一个未排序的数组中找到第k大的数,时间复杂度O(n)

    算法步骤:

    1)利用快速排序划分的思想,选定一个pivot,然后划分数组为left(全部小于pivot),right(全部大于pivot);

    2)

  • 相关阅读:
    list集合对象日期排序
    Mongodb模糊,or,and查询和日期查询
    单例模式
    代理模式
    抽象工厂模式
    java 除数运算获取两位小数
    html5 canvas 使用总结
    @MockBean 注解后 bean成员对象为 null?
    Java8 BiFunction 简单用用
    如何正确安装Ubuntu
  • 原文地址:https://www.cnblogs.com/shawshawwan/p/7910282.html
Copyright © 2011-2022 走看看