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.
题目标签:Array, Hash Table
题目给了我们一个nums array, 让我们来检查它是否有重复的数字。遍历nums array, 如果hash set 没有这个数字,就存入;有的话,直接返回true。
Java Solution:
Runtime beats 50.21%
完成日期:05/26/2017
关键词:Array, Hash Table
关键点:利用Hash set 的特性,独一性
public class Solution
{
public boolean containsDuplicate(int[] nums)
{
if(nums.length == 0 || nums == null)
return false; HashSet<Integer> unique = new HashSet<>(); for(int i=0; i<nums.length; i++)
{
if(unique.contains(nums[i]))
return true;
else
unique.add(nums[i]);
} return false;
}
}
参考资料:N/A
LeetCode 算法题目列表 - LeetCode Algorithms Questions List
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 ...
- LeetCode 219. Contains Duplicate II (包含重复项之二)
Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...
- [LeetCode] Contains Duplicate 包含重复值
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
- 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 ...
- 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 解题报告
题目要求 Given an array of integers, find if the array contains any duplicates. Your function should ret ...
- 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 ...
随机推荐
- Linux下的定时任务 - Cron服务
最近搞咕自己的笔记系统,虽然现在是个人的使用,对于数据库的数据还是比较少,但是安全还是一个我必须注意的东西. (特别是前段时间中了比特币的病毒之后,更是让我关注了我的主机的安全的问题.) 今天的随记是 ...
- bom是什么?
- python实现裴波那契数列
def Fib(n): ''' 假定序号为0或者1,返回1,序号为2时返回2 ''' before = 1 after = 1 for i in range(n): before, after = a ...
- Thinkphp3.2版本使用163邮箱发(验证码)邮件
今天忽然想写一个用户修改密码的功能,又没有短信接口,只能选择用邮箱发送验证码啦,穷啊,没办法,哈哈,以下为正文. ------------------------------------------- ...
- [AHOI2001]彩票摇奖
[AHOI2001]彩票摇奖 题目描述 为了丰富人民群众的生活.支持某些社会公益事业,北塔市设置了一 项彩票.该彩票的规则是: (1) 每张彩票上印有 7 个各不相同的号码,且这些号码的取指范围为 1 ...
- shell二位数组——终端字符下降动画
猜想:Shell支持关联数组,可以利用关联数组模拟二维数组. [验证猜想] #!/bin/bash array[1,1]=1 array[2,1]=2 array[3,1]=3 for i in `s ...
- vue路由跳转 vue-router的使用
1.路由对象和路由匹配 路由对象,即$router会被注入每个组件中,可以利用它进行一些信息的获取.如 属性 说明 $route.path 当前路由对象的路径,如'/view/a' $rotue.pa ...
- 云计算---OpenStack Neutron详解
简介: neutron是openstack核心项目之一,提供云计算环境下的虚拟网络功能 OpenStack网络(neutron)管理OpenStack环境中所有虚拟网络基础设施(VNI),物理网络基础 ...
- cors解决ajax请求跨域问题
Access-Control-Allow-Origin: * 适用tomcat部署的项目 在web.xml里添加以下内容 <filter> <filter-name>CorsF ...
- JavaWeb基础之JdbcUtils工具类final
JdbcUtils工具类3.0最终版,添加了事务相关功能和释放链接.最终版本可以直接打成jar包,在后面的基本项目都会使用该工具类 1. JdbcUtils代码 /** * 最终版 * @author ...