LeetCode--Contains Duplicate
2015-11-03 11:23
429 查看
题目:
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.
代码:
运用unordered_map,将每个数一次加入map中,如果他出现过,直接返回true,遍历完都没出现则返回false
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.
代码:
bool containsDuplicate(vector<int>& nums) { unordered_map<int, bool> a; for(int i = 0; i < nums.size(); ++i) { if(!a.count(nums[i])) { a[nums[i]] = true; } else { return true; } } return false; }
运用unordered_map,将每个数一次加入map中,如果他出现过,直接返回true,遍历完都没出现则返回false
相关文章推荐
- Daily Scrum 11.3
- SOJ 2505 The Country Fair
- SDK Manager更新出现Failed to fetch URL http://dl-ssl.google.com/android/repository/repository-10.xml
- RAID0 RAID1 RAID5 RAID10区别
- open failed: ENOENT (No such file or directory). (目录层级问题)
- There were no servers available to complete the specified operation
- LeetCode70——Climbing Stairs
- 关于io流下载文件经常遇到open failed: EISDIR (Is a directory)
- 没有什么好神秘的: wait_on_page_bit
- uva 11374 - Airport Express 机场快线 (单源最短路+枚举)
- poj Drainage Ditches 1273 (最大流入门)
- Delete Duplicate Emails---easy
- ADB server didn't ACK fail to start daemon解决方案
- Daily Scrumming* 2015.11.2(Day 14)
- toj 3072 Train Order
- 线程:sleep()、wait()、yield()和join()方法
- poj 1363Rails
- hdu1789 Doing Homework again (贪心+优先队列)
- AIDL进阶
- adb server is out of date. killing... ADB server didn't ACK * failed to star