IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    [原]LeetCode Contains Duplicate

    yangliuy发表于 2015-05-31 12:00:03
    love 0

    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搞定。

    AC Code

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




沪ICP备19023445号-2号
友情链接