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 提取目录下所有子目录的文件到指定位置
package folder; import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundExcept ...
- up61博客模版版本v1.0.0
经过两天的努力 终于把博客模板框架写出来了. 表示写模板累死了,很久没有写样式了,还是那么难搞.没有PHP写函数爽. 不管怎么样 第一版出来了.以下是部分截图.预览 当然在示例部署到项目上的时候 ,部 ...
- 关系型数据库工作原理-数据特征统计分析(翻译自Coding-Geek文章)
本文翻译自Coding-Geek文章:< How does a relational database work>.原文链接:http://coding-geek.com/how-data ...
- 系统及DB参数引发的“灾难”
最近,处理了一个系统及db参数设置不合理引发"灾难"的案例.前几天,接到某用户的紧急求助电话,同时,也收到了邮件.大概内容是:该用户某系统接连出现挂死现象,问题直指后台数据库(ai ...
- java设计模式单例模式 ----懒汉式与饿汉式的区别
常用的五种单例模式实现方式 ——主要: 1.饿汉式(线程安全,调用率高,但是,不能延迟加载.) 2.懒汉式(线程安全,调用效率不高,可以延时加载.) ——其他: 1.双重检测锁式(由于JVM底层内部模 ...
- Xamarin改变移动开发的五个理由
企业开发者不能简单的抛弃现有的桌面和Web应用,然而又不得不忙着创建各种各样的应用,没有太多的预算来开发移动版本,尤其是原生版本. 采用Xamarin,C#开发人员可以使用一份基础代码创建桌面版和移动 ...
- 使用NPOI导出Excel引发异常(IsReadOnly = “book.IsReadOnly”引发了类型“System.NotImplementedException”的异常)
前言: 本人调式npoi导入.导出试用成功后,引入到项目中,导入完美运行,但是导出怎么样都看不到现在的页面,而且浏览器和后台都没有报任务错误,让人好事纳闷,后来去调式,发现在除了一个IsReadOnl ...
- JavaScript编码规范(1)
参考的是百度公司的JS规范,分为两部分.这是第一部分 [建议] JavaScript 文件使用无 BOM 的 UTF-8 编码. 空格 [强制] 二元运算符两侧必须有一个空格,一元运算符与操作对象之间 ...
- 设计模式——命令模式(C++实现)
[root@ ~/learn_code/design_pattern/19_order]$ cat order.cpp #include <iostream> #include <s ...
- 使用TensorFlow实现回归预测
这一节使用TF搭建一个简单的神经网络用于回归预测,首先随机生成一组数据 import tensorflow as tf import numpy as np import matplotlib.pyp ...