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 ...
随机推荐
- java volatile关键字解析
volatile是什么 volatile在java语言中是一个关键字,用于修饰变量.被volatile修饰的变量后,表示这个变量在不同线程中是共享,编译器与运行时都会注意到这个变量是共享的,因此不会对 ...
- BZOJ4825 单旋
分析:一道水题,去年考场发现了特点但是不会splay维护挂了,然后现在我写了个treap. 画一画图就可以解决这道题了,自己试一下. 代码如下: #include<bits/stdc++.h&g ...
- Firefox配置文件夹详解
参考此文会帮助你更好的管理和备份Firefox配置文件,此文没有列出的文件大多是Firefox运行时生成的一些随机文件,大多无用,备份或管理配置文件时酌情删除. 在地址栏输入about:support ...
- linux中的颜色控制
\033[031m xxx \033[0m ---------------------->中间的xxx部分显示为红色,不接后面的\033[0m,则以后显示的都是红色,\033表示开始和结束 ...
- 【django之分页器】
一.什么是分页功能 二.Django的分页器(paginator) 语法: paginator = Paginator(book_list, 8) #8条一页print("count:&qu ...
- 通过Beego将之前实现的短url项目实现
正好通过这个小例子对之前了解的beego框架的基本内容进行一个简单的应用 实现的完整代码地址:https://github.com/pythonsite/go_simple_code/tree/mas ...
- angular的$scope的使用
1. 可以在scope中直接使用 // 监听日期变化 $scope.$watch('vaFilter.startEffectiveDate', function(newDate, oldDate, s ...
- Eclipse设置新建jsp文件默认模板
没有需求就没有进步,遇到问题:现在有大量的html模板页面,但是这些模板是不能和后台进行数据交互的,所以要把他们通通变成jsp页面(59个html文件),还有就是html文件转换成jsp文件的时候,前 ...
- susmote个人网站博客论坛(TexTec | 关注互联网技术,传播极客精神)
网站地址 www.susmote.com www.textec.club 欢迎您的访问
- linux --> 系统信息命令
系统信息命令 # uname -a // # 查看内核/操作系统/CPU信息 # head -n /etc/issue // # 查看操作系统版本 # cat /proc/cpuinfo // # 查 ...