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. java 压缩文件

    package folder; import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundExcept ...

  2. Tomcat 请求处理流程详解

    Overview Connector 启动以后会启动一组线程用于不同阶段的请求处理过程. Acceptor 线程组.用于接受新连接,并将新连接封装一下,选择一个 Poller 将新连接添加到 Poll ...

  3. scrapy 爬虫基础

    Scrapy是Python开发的一个快速.高层次的屏幕抓取和web抓取框架,用于抓取web站点并从页面中提取结构化的数据.Scrapy用途广泛,可以用于数据挖掘.监测和自动化测试. 安装Scrapy的 ...

  4. [解决]Linux Tomcat启动慢--Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [236,325] milliseconds

    一.背景 今天部署项目到tomcat,执行./startup.sh命令之后,访问项目迟迟加载不出来,查看日志又没报错(其实是我粗心了,当时tomcat日志还没打印完),一开始怀疑是阿里云主机出现问题, ...

  5. MySQL服务读取参数文件my.cnf的规律研究探索

    在MySQL中,它是按什么顺序或规律去读取my.cnf配置文件的呢?其实只要你花一点功夫,实验测试一下就能弄清楚,下面的实验环境为5.7.21 MySQL Community Server.其它版本如 ...

  6. EasyNetQ WithTopic过滤失效的解决方案

    RabbitMQ原理及教程:http://www.cnblogs.com/AlvinLee/p/6141834.html EasyNetQ是什么以及常见用法这里都不做讲解.可以参考这篇博文:http: ...

  7. java-StringBuffer学习笔记

    字符串是敞亮,它们的值在创建之后不能更改字符串的内容一旦发生了变化,那么马上回创建一个新的对象 public class Demo1{ public static void main(String[] ...

  8. 跨域的另一种解决方案CORS(CrossOrigin Resource Sharing)跨域资源共享

    在我们日常的项目开发时使用AJAX,传统的Ajax请求只能获取在同一个域名下面的资源,但是HTML5打破了这个限制,允许Ajax发起跨域的请求.浏览器是可以发起跨域请求的,比如你可以外链一个外域的图片 ...

  9. Python中四种样式的99乘法表

    1.常规型. #常规型 i=1 while i<=9: j=1 while j<=i: print(''%d*%d=%2d''%(i,j,i*j),end='') i+=1 #等号只是用来 ...

  10. 不可思议的纯CSS导航栏下划线跟随效果

    先上张图,如何使用纯 CSS 制作如下效果? 在继续阅读下文之前,你可以先缓一缓.尝试思考一下上面的效果或者动手尝试一下,不借助 JS ,能否巧妙的实现上述效果. OK,继续.这个效果是我在业务开发的 ...