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 ...
随机推荐
- python数据类型——列表和元组类型
列表类型(list) 定义一个列表类型很简单: l = ['a','b','c','d','e','f'] 变量l即为列表类型,可以用type方法查看: print(type(l)) 列表的增删改查 ...
- mac php 版本切换
注意:要求所有php版本都是由brew安装 一.使用brew安装php多版本方法 # brew install php56 # brew install php70 二.安装切换工具 # brew i ...
- 通过Java WebService接口从服务端下载文件
一. 前言 本文讲述如何通过webservice接口,从服务端下载文件.报告到客户端.适用于跨系统间的文件交互,传输文件不大的情况(控制在几百M以内).对于这种情况搭建一个FTP环境,增加了系统部署的 ...
- 创建Maven项目时提示web.xml is missing and <failOnMissingWebXml> is set to true错误解决方案
1. 右键点击Deployment Descriptor 2. 选择Generate Deployment Descriptor Stub P.S.下面顺便提一个小技巧: 创建动态web时先右键项目, ...
- C++学习-4
1.一个类重写了operator(),可以f()-相当于匿名对象-f()()相当于调用operator()函数 把对象名当成函数名来使用--伪函数 2.通过成员函数创建多线程 a) 可以用成员函 ...
- Unity3D UGUI强制刷新Layout(布局)组件
UGUI的Layout布局组件确实节省了我们很多代码 如果不使用Layout组件 那么光在计算UI的布局上就要花费很大的功夫 特别是动态生成其组件的时候 当然,Layout组件在大多数时候是非常好用的 ...
- linux操作日志:远程登录设置
想要远程linux服务器,首先需要在服务器上开通ssh服务,安装命令如下: sudo apt-get install openssh-server 在上图的提示中,输入“y”,继续等待安装,安装成 ...
- JAVA的18条BASE
关于Java的基础知识,实践证明学习OO,最终领悟“父类控制流程,子类实现具体的业务逻辑”的OO思想,需要的不是智商而是基础,也就是说,基础越好越快领悟,所以请每位S1学习Java的学员请牢记以下Ja ...
- .Net小白的大学四年,内含面经
大家好 我是码农阿宇,和博客园的广大兄弟一样,我们都喜欢.Net,但是你们是985/211,而我江西一所普通得不能再普通的二本大学---九江学院,大四毕业在即,英语四级未过(为什么强调这一点?见文末- ...
- [模拟赛] T2 混合图
Description Hzwer神犇最近又征服了一个国家,然后接下来却也遇见了一个难题. Hzwer的国家有n个点,m条边,而作为国王,他十分喜欢游览自己的国家.他一般 会从任意一个点出发,随便找边 ...