LeetCode 287. Find the Duplicate Number
题目:
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.
思路:
具体思路见注释。
代码1:
class Solution {
public:int findDuplicate(vector<int>& nums) {int n=nums.size()-1;int low=1;int high=n;int mid;while(low<high){//当low比high小时mid=(low+high)/2;//计算midint count=0;for(int i=0;i<=n;++i){//计算nums中比mid小于等于的数的数量 if(nums[i]<=mid){count++;}}if(count>mid){//如果count比mid大,high赋为midhigh=mid;}else{//否则low赋为mid+1low=mid+1;} }return low;//循环结束,返回的low即为重复数字}
};
输出结果: 12ms
代码2:
class Solution {
public:int findDuplicate(vector<int>& nums) {int len = nums.size();for (int i = 0; i<len - 1; ++i){for (int j = i + 1; j<len; ++j){if (nums[i] == nums[j]){//暴力方法直接两层循环return nums[i];}} }return nums[low];}
};
输出结果: 422ms
LeetCode 287. Find the Duplicate Number相关推荐
- LeetCode 287. Find the Duplicate Number (时间复杂度O(n)) + 链表判断环
LeetCode 287. Find the Duplicate Number 暴力解法 时间 O(nlog(n)),空间O(n),按题目中Note"只用O(1)的空间",照理是过 ...
- LeetCode.287 Find the Duplicate Number
题目: Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), ...
- leetcode 287. Find the Duplicate Number | 287. 寻找重复数(判断链表是否有环,并找到环的起点)
题目 https://leetcode.com/problems/find-the-duplicate-number/ 题解 题目有限制 不能修改数组元素,必须 O(1) 空间复杂度,所以 不能排序, ...
- 287. **Find the Duplicate Number
287. **Find the Duplicate Number https://leetcode.com/problems/find-the-duplicate-number/description ...
- 287. Find the Duplicate Number
Title 给定一个包含 n + 1 个整数的数组 nums,其数字都在 1 到 n 之间(包括 1 和 n),可知至少存在一个重复的整数.假设只有一个重复的整数,找出这个重复的数. 示例 1: 输入 ...
- LeetCode 287---Find the Duplicate Number
问题链接:LeetCode 287-Find the Duplicate Number 题目大意 : 找出序列中唯一一个重复出现的数字,且只能使用o(1)的额外空间 实现代码如下: public cl ...
- LintCode Find the Duplicate Number
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...
- D19:Duplicate Number(重复数字,翻译+题解)
原题:OpenJudge - 19:Duplicate Number 翻译: 描述:给定一个N个数的序列,求一个在序列中的至少出现2次的数A: 输入:第一行:一个不大于1000的正整数N : 第二行: ...
- leetcode【537】Complex Number Multiplication(复数相乘)
写在最前面:一道很常规的字符串分割的题 leetcode[537]Complex Number Multiplication Given two strings representing two co ...
最新文章
- 20行以内python代码画出各种减压图
- 详解git pull和git fetch的区别:
- 数据库分库分表、读写分离的原理和实现,以及使用场景
- 与大家分享一下2010我的找工作历程!真累啊!不过都已经结束了!
- 性能测试oracle瓶颈定位,性能测试中如何定位性能瓶颈
- c语言i++和++i程序_使用C ++程序从链接列表中消除重复项
- Bootstrap 模态框(Modal)
- (轉貼) 如何解決MegaCore IP 6.0安裝時-6001的錯誤? (IC Design) (MegaCore)
- unity, 删除animationEvent
- 判断一个字符串出现次数最多的字符,并返回这个字符和次数
- 计算机软考论文网络真题,软考历年真题在线测试系统测试与开发
- c 怎么更改计算机的默认打印机,C#Winfrom系统打印机调用/设置默认打印机
- firefly-rk3288j开发板 eDP实验之NV101WXM-N51显示屏驱动
- 红黑树如何快速调整到平衡态_快速多态
- CentOS升级PHP到8.0
- 清空MySQL单库下所有表数据 || 删除MySQL单库下所有表
- Java职责链模式详解
- 使用Authorize.net的SDK实现符合PCI标准的支付流程
- uni-app转小程序ios video不生效+视频播放不了、黑屏问题
- 电脑合上盖子不进入休眠模式的问题解决方案
热门文章
- php如何给单选框加js事件,js给元素添加绑定事件
- fianl属性 java_java基础-类的高级属性(包、final、内部类)
- python selenium_自动化测试:Selenium+Python环境搭建
- 操作系统(李治军) L10用户级线程
- 深度优先搜索(解题剑指Offer12、13)
- phpcmsV9 数据库配置文件(查找、修改)
- 新UI云开发壁纸小程序源码(新修复版带编译教程)
- html css考试题选择题,html_JavaScript_css试题
- 计算机专业3d游戏设计,史塔福郡大学3D计算机游戏设计理学硕士研究生申请要求及申请材料要求清单...
- 一、vi/vim编辑器