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.
解题思路:
这种重复的问题首先想到的就是Set了。
代码如下:
public class Solution {
public boolean containsDuplicate(int[] nums) {
Set<Integer> set = new HashSet<Integer>();
for(int i = 0; i < nums.length; i++){
if(set.contains(nums[i]))
return true;
else
set.add(nums[i]);
}
return false;
}
}
Java [Leetcode 217]Contains Duplicate的更多相关文章
- [LeetCode] 217. Contains Duplicate 包含重复元素
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
- 25. leetcode 217. Contains Duplicate
217. Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your ...
- leetcode 217. Contains Duplicate 287. Find the Duplicate Number 442. Find All Duplicates in an Array 448. Find All Numbers Disappeared in an Array
后面3个题都是限制在1-n的,所有可以不先排序,可以利用巧方法做.最后两个题几乎一模一样. 217. Contains Duplicate class Solution { public: bool ...
- LN : leetcode 217 Contains Duplicate
lc 217 Contains Duplicate 217 Contains Duplicate Given an array of integers, find if the array conta ...
- Java for LeetCode 217 Contains Duplicate
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
- LeetCode 217. Contains Duplicate (包含重复项)
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
- leetcode 217 Contains Duplicate 数组中是否有重复的数字
Contains Duplicate Total Accepted: 26477 Total Submissions: 73478 My Submissions Given an array o ...
- leetcode 217 Contains Duplicate 数组中是否有反复的数字
Contains Duplicate Total Accepted: 26477 Total Submissions: 73478 My Submissions Given an array o ...
- LeetCode 217 Contains Duplicate
Problem: Given an array of integers, find if the array contains any duplicates. Your function should ...
随机推荐
- c语言随机数
写得我自己都看不好了:大家都比较喜欢吃快餐,只需要尽快告诉读者怎么用起来就行了.不想听啰啰嗦嗦说一堆,然后例程还特别麻烦 so: 1.基本 int seed = time(0);//#include ...
- splObjectStroge的作用,实例化一个数组
PHP SPL SplObjectStorage是用来存储一组对象的,特别是当你需要唯一标识对象的时候. PHP SPL SplObjectStorage类实现了Countable,Iterator, ...
- mybatis + postgresql 遇到的问题
org.postgresql.util.PSQLException: ERROR: relation "circlefence" does not exist 这个问题是数据库表 ...
- 实验五 Java网络编程及安全
北京电子科技学院 实 验 报 告 课程:移动平台应用开发实践 班级:201592 姓名:曾俊宏 学号:20159210 成绩:___________ 指导老师: ...
- poj City Horizon (线段树+二分离散)
http://poj.org/problem?id=3277 City Horizon Time Limit: 2000MS Memory Limit: 65536K Total Submissi ...
- shell复习笔记----用户管理
$ who 可以知道系统上有多少登陆 $who |wc -l 计算用户个数 注意:|是管道符号,可以在两个程序之间建立管道(pipeline):who 的输出,成了 wc 的输入, wc 所列出 ...
- 如何编写敏捷开发中的user story
http://blog.csdn.net/chengyb74/article/details/4762247 对于敏捷开发来说,User Story是开发的基础,它不同于传统的瀑布式开发方式,而是把原 ...
- Firefly Http通信简单介绍
原地址:http://www.9miao.com/question-15-54042.html 首先创建firefly工程,firefly-admin.py createproject httptes ...
- CSU1326+背包+并查集
先预处理出有多少个任务即可 #include<stdio.h> #include<stdlib.h> #include<string.h> #include< ...
- 一周一话题之四(JavaScript、Dom、jQuery全面复习总结<Dom篇>)
-->目录导航 一. 初探Dom 1. Dom介绍 二. Dom基础 1. window顶级对象 2. body.document对象事件 3. 通用的HTML元素的事件 4. 冒泡事件 5. ...