Leet Code OJ 217. Contains Duplicate [Difficulty: Easy]
来源:互联网 发布:文字特效软件下载 编辑:程序博客网 时间:2024/04/29 13:42
题目:
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.
思路分析:
题意是给定一个整形数组,如果里面的元素都不重复,返回false,否则返回true。以下的做法使用map作为临时存储,记录元素是否出现。
代码实现(时间复杂度O(n)):
public class Solution { public static boolean containsDuplicate(int[] nums) { Map<Integer, Boolean> map = new HashMap<>(); for (int num : nums) { Object o = map.get(num); if (o == null) { map.put(num, true); } else { return true; } } return false; }}
1 0
- Leet Code OJ 217. Contains Duplicate [Difficulty: Easy]
- Leet Code OJ 219. Contains Duplicate II [Difficulty: Easy]
- Leet Code OJ 292. Nim Game [Difficulty: Easy]
- Leet Code OJ 242. Valid Anagram [Difficulty: Easy]
- Leet Code OJ 231. Power of Two [Difficulty: Easy]
- Leet Code OJ 258. Add Digits [Difficulty: Easy]
- Leet Code OJ 226. Invert Binary Tree [Difficulty: Easy]
- Leet Code OJ 283. Move Zeroes [Difficulty: Easy]
- Leet Code OJ 169. Majority Element [Difficulty: Easy]
- Leet Code OJ 263. Ugly Number [Difficulty: Easy]
- Leet Code OJ 100. Same Tree [Difficulty: Easy]
- Leet Code OJ 70. Climbing Stairs [Difficulty: Easy]
- Leet Code OJ 202. Happy Number [Difficulty: Easy]
- Leet Code OJ 101. Symmetric Tree [Difficulty: Easy]
- Leet Code OJ 112. Path Sum [Difficulty: Easy]
- Leet Code OJ 171. Excel Sheet Column Number [Difficulty: Easy]
- Leet Code OJ 168. Excel Sheet Column Title [Difficulty: Easy]
- Leet Code OJ 38. Count and Say [Difficulty: Easy]
- Linux之vi命令
- Spring相关
- Java开发中的23种设计模式详解(转)
- 欢迎使用CSDN-markdown编辑器
- [设计模式] 装饰者模式Decorator
- Leet Code OJ 217. Contains Duplicate [Difficulty: Easy]
- 【html5手游开发】虚拟摇杆及虚拟按键的开发
- MySQL的外键插入
- linux系统创建SFTP用户及设置其chroot权限
- 第十三章编程练习(4)
- iOS 开发 -- Swift (十) 重载构造函数
- iOS 常用宏定义
- 如何解决掉字符
- onload