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. C++通过COM接口操作PPT

    一. 背景 在VS环境下,开发C++代码操作PPT,支持对PPT模板的修改.包括修改文本标签.图表.表格.满足大多数软件生成PPT报告的要求,先手工创建好PPT模板,在程序中修改模板数据. 二. 开发 ...

  2. SDN 网络系统之 Mininet 与 API 详解

    SDN 网络系统之 Mininet 与 API 详解 Mininet 是轻量级的软件定义网络系统平台,同时提供了对 OpenFlow 协议的支持.本文主要介绍了 Mininet 的相关概念与特性,并列 ...

  3. Appserv(Apache) 配置ssl证书

    一:打开httpd.conf文件,移除注释的行: Include conf/extra/httpd-ahssl.conf LoadModule ssl_module modules/mod_ssl.s ...

  4. ZOJ3946:Highway Project(最短路变形)

    本文转载自:http://www.javaxxz.com/thread-359442-1-1.html Edward, the emperor of the Marjar Empire, wants ...

  5. PAT乙级-1063. 计算谱半径(20)

    在数学中,矩阵的"谱半径"是指其特征值的模集合的上确界.换言之,对于给定的n个复数空间的特征值{a1+b1i, ..., an+bni},它们的模为实部与虚部的平方和的开方,而&q ...

  6. 1111 WordReplace

    #include<iostream> #include<string> using namespace std; int main() { string sa,sb,s; wh ...

  7. Unity3D打包 将发布的exe文件打包成一个Windows安装文件(自解压文件)

    Unity打包Standalone时 会出现一个exe文件和一个data文件夹 可是我们平常见过的软件 基本没有这种像这种结构的 一般都是一个安装文件,然后点击安装,选择路径,生成快捷方式- 本篇博客 ...

  8. Unix 让进程安全地退出

    终止一个进程有很多方法(暂只说linux环境):前台运行的进程,如果没有提供退出功能,我们通常会Ctrl+C进行终止:后台或守护进程,如果也没有提供退出命令啥的,咱通常会kill掉:此外还有类似关机或 ...

  9. win7开通共享步骤

    win7开通共享步骤 2017-10-09    11:12:09 个人原创博客,允许转载,转载请注明作者及出处,否则追究法律责任 1,开通来宾账户 2,为来宾账户创建一个空密码 右键我的电脑,管理, ...

  10. iOS 用户体验之音频

    早期某知名公司的应用有这么一个问题,如果我在听音乐时打开该应用,播放一段小视频,视频播放完成之后,音乐没有继续播放.这个问题被很多用户吐槽,很久以后,该公司终于修复了这个问题. 无论声音是应用体验的重 ...