Find the Duplicate Number
2015-10-01 08:42
344 查看
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one.
Note:
You must not modify the array (assume the array is read only).
You must use only constant, O(1) extra space.
Your runtime complexity should be less than
There is only one duplicate number in the array, but it could be repeated more than once.
Runtime: 28ms
Note:
You must not modify the array (assume the array is read only).
You must use only constant, O(1) extra space.
Your runtime complexity should be less than
O(n2).
There is only one duplicate number in the array, but it could be repeated more than once.
Runtime: 28ms
class Solution { public: int findDuplicate(vector<int>& nums) { if(nums.empty()) return 0; unordered_map<int, int> m; for(int i = 0; i < nums.size(); i++){ if(m.find(nums[i]) == m.end()) m[nums[i]] = i; else return nums[i]; } } };
相关文章推荐
- Junit测试时,如何截获到Console的输出
- uva 839
- 创客张江 招募
- 代码面试之串
- LeetCode-Evaluate Reverse Polish Notation
- JS处理json日期格式化问题
- qt 委托 delegate
- Longest Palindromic Substring
- [教程]如何使用V7包中ActionBar(Eclipse版)
- ISA TEST Writeup
- http://code.662p.com/view/5141.html
- 一起学习android图像缩放资源 (27)
- Combination Sum III
- 转:DLL如何导出C++的类
- 虚拟内存
- Error during bitstream generation
- Ubuntu 15.10 会从此摘掉“乏味的升级版”这顶帽子吗?
- 虚拟内存
- 代码大全2读后感(1)
- JS日期格式化之javascript Date format