LeetCode.217(219) Contains Duplicate && II

来源:互联网 发布:sql 查询列重复的数据 编辑:程序博客网 时间:2024/06/02 03:26

题目217:

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.

分析:

解法1:

class Solution {    public boolean containsDuplicate(int[] nums) {        //给定数组,判定其中是否包含重复数据,若存在返回true,否则false        //肯定有坑        if(nums.length==0||nums==null)return false;                Arrays.sort(nums);        for(int i=0;i<nums.length-1;i++){            if(nums[i]==nums[i+1]){                return true;            }        }        return false;    }}
解法2(推荐):

class Solution {    public boolean containsDuplicate(int[] nums) {        //给定数组,判定其中是否包含重复数据,若存在返回true,否则false        //解法2:使用Set集合实现        //使用set集合实现        //set集合由HashSet和TreeSet继承实现        //set集合不同于List集合,set(存储和取出顺序不一致)唯一        // List(存取和取出顺序一致)可重复        Set<Integer> set = new HashSet<>();        for(int i=0;i<nums.length;i++){            if(!set.add(nums[i])){                //add方法如果包含,返回true                return true;            }        }        return false;    }}

题目219:

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.

分析:

解法1:

class Solution {    public boolean containsNearbyDuplicate(int[] nums, int k) {        //给定数组和k,找出是否存在两个相同数之间的下标差值不能大于k                HashMap<Integer,Integer> hm=new HashMap<Integer,Integer>();        for(int i=0;i<nums.length;i++){            if(hm.containsKey(nums[i])){                //包含相同值                int oldIndex=hm.get(nums[i]);                if(i-oldIndex<=k){                    return true;                }else{                    //防止出现,后面有更小的距离,所以更新为最新下标                    hm.put(nums[i],i);                }                                }else{                //不包含相同值,直接存                hm.put(nums[i],i);            }        }        return false;            }}

解法2(推荐):

class Solution {    public boolean containsNearbyDuplicate(int[] nums, int k) {        //给定数组和k,找出是否存在两个相同数之间的下标差值不能大于k        //思路:从当前下标开始,判定给定长度内,从尾部往头部遍历                if(k>=35000)return false;                for(int i=0;i<nums.length;i++){            //取尾部,跟数组长度比较,防止头尾有相同            for(int j=Math.min(nums.length-1,i+k);j>i;j--){                if(nums[i]==nums[j])return true;            }        }        return false;    }}


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 华为mate9相机无法对焦怎么办 新疆外地电信卡信号差怎么办 华为手机搜索不到wifi怎么办 华为浏览器恢复只有一个页面怎么办 华为手机触摸屏没反应怎么办 快递不给送上楼怎么办 华为荣耀手机声音小怎么办 华为手机来电铃声小怎么办 s弯出来时老压线怎么办 苹果7p手机弯了怎么办 小米手机摔弯了怎么办 华为畅享5没声音怎么办 掌阅语音闪退怎么办 华为mate开屏成排线怎么办 华为mate8电池坏了怎么办 8plus拍照不清晰怎么办 荣耀手环3丢了怎么办 华为mate9手机声音小怎么办 华为麦芒6丢了怎么办 华为麦芒4无法访问移动网络怎么办 自拍时屏幕是白的怎么办 华为麦芒5手机音量小怎么办 小米5x玩王者卡怎么办 小米5x打王者卡怎么办 华为荣耀10卡顿怎么办 麦芒6记不得密码怎么办 华为麦芒4镜头碎了怎么办 三星s5出像网的信号怎么办 华为手机进海水资料怎么办 华为手机进海水了怎么办 苹果手机玩王者荣耀卡怎么办 玩王者荣耀闪屏怎么办 王者荣耀太卡了怎么办 想卖王者号qq怎么办 小米4电视发热严重怎么办 三星玩王者荣耀卡怎么办 华为麦芒5网络差怎么办 华为麦芒无线信号不好怎么办 华为麦芒6网络不好怎么办 华为麦芒6信号差怎么办 华为手机无线网信号差怎么办