LeetCode OJ 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.
这个问题比较简单,判断一个数组里的是否有重复出现的数字。代码简单如下:
public class Solution {
public boolean containsDuplicate(int[] nums) {
Map<Integer,Integer> map = new HashMap<>(0); for(int num : nums){
if(map.containsKey(num)) return true;
map.put(num,0);
}
return false;
}
}
public class Solution {
public boolean containsDuplicate(int[] nums) {
if(nums == null || nums.length <= 1) return false; Set<Integer> set = new HashSet<>();
for(int num : nums) {
if(!set.add(num)) return true;
}
return false;
}
}
LeetCode OJ 217.Contains Duplicate的更多相关文章
- 【一天一道LeetCode】#217. Contains Duplicate
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】217. Contains Duplicate (2 solutions)
Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your funct ...
- 【LeetCode】217. Contains Duplicate 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典统计词频 使用set 排序 日期 [LeetCo ...
- LeetCode【217. Contains Duplicate】
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
- LeetCode OJ 220.Contains Duplicate 3
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...
- LeetCode OJ 219.Contains Duplicate 2
Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...
- 【LeetCode】217. Contains Duplicate
题目: Given an array of integers, find if the array contains any duplicates. Your function should retu ...
- LeetCode OJ:Contains Duplicate III(是否包含重复)
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...
- LeetCode OJ:Contains Duplicate(是否包含重复)
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
随机推荐
- vs2008编译FileZilla服务端源码
vs2008编译FileZilla服务端源码 FileZilla服务端下载地址:https://download.filezilla-project.org/server/.FileZilla服务端源 ...
- 利用StringBuffer向字符串特定的重复子字符串插入数据
public class InsertDetail { public void insertInvoiceDetail(StringBuffer sb, String Label, String ...
- C-Free 5.0编译失败问题解决办法
解决关于C-Free 5.0编译时提示错误:[Error] undefined reference to `__dyn_tls_init_callback' 解决办法: 因为错误提示的路径是C:\Mi ...
- 在hadoop 的任务中设置 map数量
试验了一下: 调整mapred-site.xml中mapred.min.split.size的值可以改变map的数量 首先设置了hdfs-site.xml中的dfs.block.size为20M,测试 ...
- ZOJ 1926 Guessing Game
#include<cstdio> ],s2[]; ]; ]; int bz; int main() { int n,i; while(~scanf("%d",& ...
- Eight
Eight 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043/http://acm.split.hdu.edu.cn/showproblem.ph ...
- magento数据查询
一.查询出所有的数据: 1.以mysql查询语句在magento里执行,以此来查询你所需要的语句! $read = Mage::getSingleton('core/resource')->ge ...
- referencedColumnName
In JPA there is a an attribute called referencedColumnName that can be set on @JoinColumn, @PrimaryK ...
- Intellij Idea web项目的部署配置[转]
原文地址:http://blog.csdn.net/z69183787/article/details/41416189 1.前言 2.项目配置(Project Structure) 2.1 Proj ...
- java 读取excel 将数据插入到数据库
import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.sql.Con ...