zoukankan      html  css  js  c++  java
  • LeetCode 217. Contains Duplicate

    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.

    分析:

    HashSet?

    first try:

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

    result:

    second:

    import java.util.HashSet;
    
    class Solution {
        public boolean containsDuplicate(int[] nums) {
            HashSet<Integer> hashSet = new HashSet<Integer>();
            
            for(int i=0; i<nums.length; i++) {
                int num = nums[i];
                if(hashSet.contains(num) ) {
                    return true;
                } else {
                    hashSet.add(num);
                }
            }
            
            return false;
        }
    }

    result:

    the same.

    third:

    import java.util.HashSet;
    
    class Solution {
        public boolean containsDuplicate(int[] nums) {
            HashSet<Integer> hashSet = new HashSet<Integer>();
            
            int length = nums.length;
            for(int i=0; i<length; i++) {
                int num = nums[i];
                if(hashSet.contains(num) ) {
                    return true;
                } else {
                    hashSet.add(num);
                }
            }
            
            return false;
        }
    }

    result:

    anylysis:

    看了solution里的代码,并执行了一下,发现比我的代码还慢。

    import java.util.HashSet;
    
    public class Solution {
        public boolean containsDuplicate(int[] nums) {
        Set<Integer> set = new HashSet<Integer>();
        for (int x: nums) {
            if (set.contains(x)) {
                return true;   
            } else {
                set.add(x);
            }
           
        }
        return false;
        }
    }

    result:

    32.96%

    import java.util.HashSet;
    
    public class Solution {
        public boolean containsDuplicate(int[] nums) {
            Set<Integer> set = new HashSet<Integer>();
            int length = nums.length;
            for(int i=0; i<length; i++) {
                if (set.contains(nums[i])) {
                    return true;   
                } else {
                    set.add(nums[i]);
                }
           
            }
            return false;
        }
    }

    17.88%

    发现慢的原因在于Set,如果是HashSet速度会变快。

    for 和foreach循环速度在都是HashSet时没有变化;在是Set时有变化。

    发现这种优化费时费力,放弃。

    总结:

    import java.util.HashSet;

    public class Solution {
        public boolean containsDuplicate(int[] nums) {
        Set<Integer> set = new HashSet<Integer>();
        for (int x: nums) {
            if (set.contains(x)) {
                return true;   
            } else {
                set.add(x);
            }
           
        }
        return false;
        }
    }

  • 相关阅读:
    SpringBoot中mybatis配置自动转换驼峰标识没有生效
    spring boot 配置动态刷新
    读书笔记——spring cloud 中 HystrixCommand的四种执行方式简述
    spring cloud 加入配置中心后的 部分 配置文件优先级
    spring boot 服务 正确关闭方式
    CentOS 6.4 安装 rabbitmq(3.6.15)
    CentOS 6.4 配置DNS
    CentOS 查看系统版本号
    服务治理的技术点
    【转载】C#中使用Average方法对List集合中相应元素求平均值
  • 原文地址:https://www.cnblogs.com/hzg1981/p/8875514.html
Copyright © 2011-2022 走看看