287. Find the Duplicate Number
来源:互联网 发布:客户标签大数据图 编辑:程序博客网 时间:2024/04/29 18:07
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
O(n2)
.
- There is only one duplicate number in the array, but it could be repeated more than once.
class Solution {public: int findDuplicate(vector<int>& nums) { int left = 0; int right = nums.size()-1; while(left < right){ int mid = left + (right - left) /2; int cnt1 = 0; for(int i = 0; i < nums.size(); i++){ if(nums[i]<= mid) cnt1++; } if(cnt1<=mid) left = mid +1; else right = mid; } return left; }};
0 0
- 287. Find the Duplicate Number
- 287. Find the Duplicate Number
- 287. Find the Duplicate Number
- 287. Find the Duplicate Number
- 287. Find the Duplicate Number
- 287. Find the Duplicate Number
- 287. Find the Duplicate Number
- 287. Find the Duplicate Number
- 287. Find the Duplicate Number
- 287. Find the Duplicate Number
- 287.Find the Duplicate Number
- 287. Find the Duplicate Number
- 287. Find the Duplicate Number
- 287. Find the Duplicate Number
- 287. Find the Duplicate Number
- 287. Find the Duplicate Number
- 287. Find the Duplicate Number
- 287. Find the Duplicate Number
- java项目命名规范
- java环境安装(win7)
- 给vim添加代码提示YouCompleteMe
- inspect模块使用
- qq javax.mail.authenticationfailedexception
- 287. Find the Duplicate Number
- Android 静默安装/后台安装
- 机器学习之——多项式回归和正规方程
- 鼠标事件mousemove、mouseover、mouseout、mouseenter、mouseleave
- Sencha Cmd 6 和 Ext JS 6 指南文档(部分官方文档中文翻译)
- c#用法总结
- 旅游预算
- javascript设计模式--工厂方法模式
- 第一次使用CSDN的手机客户端