• 217.存在重复元素



    这道题采用的是hashset 如果重复的话就return true
    方法不难

     public boolean containsDuplicate(int[] nums) {
    		HashSet<Integer> hs = new HashSet<Integer>();
    		for(int i = 0 ;i< nums.length;i++) {
    			if(hs.contains(nums[i])) {
    				return true;
    			}
    			else {
    				hs.add(nums[i]);
    			}
    		} 
    		return false;
    	    }
    

    另外一种是排序完在进行判断是否有重复的

     public static boolean sContainsDuplicate(int[] nums)
        {
            Arrays.sort(nums);
            if (nums.length <= 1)
            {
                return false;
            }
            int tem = nums[0];
            for (int i = 1; i < nums.length; i++)
            {
                if (nums[i] == tem)
                {
                    return true;
                } else
                {
                    tem = nums[i];
                }
            }
            return false;
        }
    
  • 相关阅读:
    3-2 案例准备工作
    3-1 Git下载与安装
    3-1 案例环境初始化
    1-2+并发编程初体验
    Linux
    HTTP
    Linux
    HTML
    Linux 命令
    MySQL
  • 原文地址:https://www.cnblogs.com/cznczai/p/11150445.html
Copyright © 2020-2023  润新知