LeetCode & Q217-Contains Duplicate-Easy
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的更多相关文章
- [LeetCode] 036. Valid Sudoku (Easy) (C++)
指数:[LeetCode] Leetcode 解决问题的指数 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 036. ...
- [array] leetcode - 53. Maximum Subarray - Easy
leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...
- [LeetCode] 217. Contains Duplicate 包含重复元素
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
- [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 ...
- [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 ...
- 【leetcode】Contains Duplicate & Rectangle Area(easy)
Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your funct ...
- (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 ...
- (easy)LeetCode 217.Contains Duplicate
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
- [LeetCode] Find the Duplicate Number 寻找重复数
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...
- leetcode@ [316] Remove Duplicate Letters (Stack & Greedy)
https://leetcode.com/problems/remove-duplicate-letters/ Given a string which contains only lowercase ...
随机推荐
- Android学习之AutoCompleteTextView和MultiAutoCompleteTextView
转自:http://blog.csdn.net/qq_28468727/article/details/52258409 AutoCompleteTextView.MultiAutoCompleteT ...
- 洛谷 P1017 进制转换
推荐洛谷 题目描述 我们可以用这样的方式来表示一个十进制数: 将每个阿拉伯数字乘以一个以该数字所处位置的(值减1)为指数,以10为底数的幂之和的形式.例如:123可表示为 1*10^2+2*10^1+ ...
- 关于“应用程序无法启动,因为应用程序的并行配置不正确。请参阅应用程序事件日志,或使用命令行sxstrace.exe工具”问题的解决方法
今天打开QQ管家加速版的时候突然出现了这个错误,百度了下说是系统缺少Microsoft Visual C++ 20XX(运行库),下载这个安装即可解决问题.
- WordPress文章首行缩进
WordPress后台编辑文章的时候会自动删除多余的空格,也就是说,你在后台编辑文章的时候添加的一些空格和换行在前台都是看不见的,都是被WordPress忽略了的,今天就讲讲怎么给所有文章添加首行缩进 ...
- WordPress让文本小工具支持简码
WordPress 的 “文本” 小工具是非常常用的,可以添加一些自定义的文本或者 Html 代码.但很多时候,我们需要在文本小工具里使用简码来添加一些更加丰富的内容. 默认情况下,文本小工具是不支持 ...
- tp框架-----文件上传
之前也做过文件上传,现在学了tp,用tp怎么做呢? 第一步:做一个Wenjian控制器: <?php namespace Ceshi\Controller; use Think\Controll ...
- Ubuntu Mininet环境搭建
我们通过源码方式搭建mininet仿真平台,使用git下载mininet源码 git clone git://github.com/mininet/mininet 下载完成之后,使用下面命令选择安装版 ...
- SAP OLE中常用的一些方法和属性
1.ole中如何保存和退出. call method of sheetname = filepath # =. call method of applicationname 'quit'. 2.给sh ...
- sentinel监控redis高可用集群(一)
一.首先配置redis的主从同步集群. 1.主库的配置文件不用修改,从库的配置文件只需增加一行,说明主库的IP端口.如果需要验证的,也要加多一行,认证密码. slaveof 192.168.20.26 ...
- vmware虚拟机和网络中的桥接和NAT
vmware虚拟机和网络中的桥接和NAT 有许多人在网上回答类似的问题,但大多说的不够简单,且互相抄袭的嫌疑很大,这里我尽自己努力把问题说的明白一些 首先解释一下什么是NAT(network addr ...