Array Hash Table

Description:

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.

my Solution:

public class Solution {
public boolean containsDuplicate(int[] nums) {
HashSet temp = new HashSet();
for (int num : nums) {
temp.add(num);
}
return temp.size() < nums.length;
}
}

HashSet因为不会存储重复元素,可以在这里使用,另一种用法就是调用HashSet.contains()

Other Solution:

public boolean containDuplicate(int[] nums) {
Array.sort(nums);
for (int i = 1; i < nums.length; i++) {
if (nums[i] == nums[i-1]) {
return true;
}
}
return false;
}

LeetCode & Q217-Contains Duplicate-Easy的更多相关文章

  1. [LeetCode] 036. Valid Sudoku (Easy) (C++)

    指数:[LeetCode] Leetcode 解决问题的指数 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 036. ...

  2. [array] leetcode - 53. Maximum Subarray - Easy

    leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...

  3. [LeetCode] 217. Contains Duplicate 包含重复元素

    Given an array of integers, find if the array contains any duplicates. Your function should return t ...

  4. [LeetCode] 219. Contains Duplicate II 包含重复元素 II

    Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...

  5. [LeetCode] 220. Contains Duplicate III 包含重复元素 III

    Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...

  6. 【leetcode】Contains Duplicate & Rectangle Area(easy)

    Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your funct ...

  7. (easy)LeetCode 219.Contains Duplicate II

    Given an array of integers and an integer k, find out whether there there are two distinct indices i ...

  8. (easy)LeetCode 217.Contains Duplicate

    Given an array of integers, find if the array contains any duplicates. Your function should return t ...

  9. [LeetCode] Find the Duplicate Number 寻找重复数

    Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...

  10. leetcode@ [316] Remove Duplicate Letters (Stack & Greedy)

    https://leetcode.com/problems/remove-duplicate-letters/ Given a string which contains only lowercase ...

随机推荐

  1. mysql url 连接配置的一个小坑。 工作中不会遇到。 学习的时候会

    <property name="driverClassName"> <value>com.mysql.jdbc.Driver</value> & ...

  2. 24时区,GMT,UTC,DST,CST时间详解

    全球24个时区的划分      相较于两地时间表,可以显示世界各时区时间和地名的世界时区表(World Time),就显得精密与复杂多了,通常世界时区表的表盘上会标示着全球24个时区的城市名称,但究竟 ...

  3. ASP.NET Core 一步步搭建个人网站(7)_Linux系统移植

    摘要 考虑我们为什么要选择.NET Core? 因为它面向的是高性能服务器开发,抛却了 AspNet 的臃肿组件,非常轻量,加上微软的跨平台战略,对 Docker 的亲和性,对于开发人员也非常友好,所 ...

  4. UML 中extend和include的区别

    在UML用例图中有两种关系——包含和扩展,容易混淆,下面通过一张表来区别一下这两种关系.

  5. JAVA蓝桥杯黄金分割数,涉及到bigdecimal

    import java.math.BigDecimal; public class test { public static void main(String[] args) { BigDecimal ...

  6. C语言编程之道--读书笔记

    C语言语法 const int nListNum =sizeof(aPrimeList)/sizeof(unsigned);//计算素数表里元素的个数 1:#define INM_MAX 32767 ...

  7. Linux中jdk的安装和环境变量的配置

    我安装的linux系统版本是RedHat4 64位,jdk版本为jdk-6u10-linux-i586.bin 1.首先找到安装包的位置->进入此文件夹,此bin格式的文件可用./命令直接进行安 ...

  8. docker学习系列(五):使用docker创建集成服务--lnmp

    在掌握了docker的基本命令之后,我也是想着去用docker做一点实际的配套环境,就拿自己最常用的lnmp环境来做测试.配套环境运行的顺序依次是mysql->php->nginx,至于为 ...

  9. 1-2 hibernate主配置文件hibernate.cfg.xml详解

    详 http://www.cnblogs.com/biehongli/p/6531575.html Hibernate的主配置文件hibernate.cfg.xml 1:Hibernate的主配置文件 ...

  10. js先后对某个js对象内的两个属性排序

    需求 列表中先根据某id进行排序,然后id相同的再按某属性进行排序.最终显示效果如图所示: 实现代码 var data.items = [ {'brand_id':1,'farm_id':2}, {' ...