zoukankan      html  css  js  c++  java
  • 217. Contains Duplicate数组重复元素 123

    [抄题]:

    Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.

     [暴力解法]:

    时间分析:n^2

    空间分析:1

     [优化后]:

    时间分析:nlgn

    空间分析:1

    [奇葩输出条件]:

    [奇葩corner case]:

    [思维问题]:

    [一句话思路]:

     双重指针必须联想到用前向窗口优化

    [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

    [画图]:

    [一刷]:

    [二刷]:

    [三刷]:

    [四刷]:

    [五刷]:

      [五分钟肉眼debug的结果]:

    [总结]:

     双重指针必须联想到用前向窗口优化

    [复杂度]:Time complexity: O(nlgn) Space complexity: O(n)

    [英文数据结构或算法,为什么不用别的数据结构或算法]:

    [关键模板化代码]:

    [其他解法]:

    [Follow Up]:

    [LC给出的题目变变变]:

     [代码风格] :

    class Solution {
        public boolean containsDuplicate(int[] nums) {
            //cc
            if (nums.length == 0 || nums == null) {
                return false;
            }
            //ini = sort
            Arrays.sort(nums);
            //for 
            for (int i = 1; i < nums.length; i++) {
                if (nums[i] == nums[i - 1]) {
                    return true;
                }
            }
            return false;
        }
    }
    View Code

    [抄题]:

    Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k.

     [暴力解法]:

    时间分析:

    空间分析:

     [优化后]:

    时间分析:

    空间分析:

    [奇葩输出条件]:

    [奇葩corner case]:

    [思维问题]:

    不能直接-1定范围了,只能用hashset

    [一句话思路]:

    每次都往窗口中去掉左边界,添加自己

    [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

    [画图]:

    [一刷]:

    1. 强化区别意识:元素一直要加,index > k 才加
    2. +1, -1需要试试

    [二刷]:

    [三刷]:

    [四刷]:

    [五刷]:

      [五分钟肉眼debug的结果]:

    [总结]:

    1. 强化区别意识:元素一直要加,index > k 才加

    [复杂度]:Time complexity: O(n) Space complexity: O(n)

    [英文数据结构或算法,为什么不用别的数据结构或算法]:

    [关键模板化代码]:

    [其他解法]:

    [Follow Up]:

    [LC给出的题目变变变]:

     [代码风格] :

    class Solution {
        public boolean containsNearbyDuplicate(int[] nums, int k) {
            //cc
            if (nums.length == 0 || nums == null) {
                return false;
            }
            
            //ini = sort
            Set<Integer> set = new HashSet<>();
            
            //for
            for (int i = 0; i < nums.length; i++) {
                if (i > k) {
                    set.remove(nums[i - k - 1]);
                }
                if (! set.add(nums[i])) {
                        return true;
                    }
            }
            //return
            return false;
        }
    }
    View Code
  • 相关阅读:
    管理页面的类 PageHelper
    接下来打算写一下visual stuido 2013使用git进行远端管理。
    转一下网上找来的tortoise git不用每次都输入邮箱和密码的方法。备查看
    tortoise git使用 git版本库的rsa key来进行ssh连接
    2015年4月1日 14:36:56 EF 主从表更新
    Oop分析方法
    Knative 实战:基于 Knative Serverless 技术实现天气服务-下篇
    超大规模商用 K8s 场景下,阿里巴巴如何动态解决容器资源的按需分配问题?
    从零开始入门 K8s | 可观测性:你的应用健康吗?
    Knative 暂时不会捐给任何基金会 | 云原生生态周报 Vol. 22
  • 原文地址:https://www.cnblogs.com/immiao0319/p/8848042.html
Copyright © 2011-2022 走看看