方法一:map

时间复杂度 O(n)空间复杂度O(n)

方法二:交换 [2,3,4,5,1,1]

class Solution {
public:int findDuplicate(vector<int>& nums) {for(int i = 0; i < nums.size(); i++){while(nums[i] != i+1){if(nums[i] == nums[nums[i]-1]){return nums[i];}swap(nums[i], nums[nums[i]-1]);}   }}
};

时间复杂度O(n) 空间复杂度O(1)

方法三:二分查找,统计小于等于每个数的个数,比如统计小于等于5的个数,如果小于等于5的个数不是5说明5或者之前有重复的

class Solution {
public:int findDuplicate(vector<int>& nums) {int len = nums.size();int st = 1;int ed = len;int count = 0;int mid;while (st <= ed){count = 0;mid = st + (ed - st)/2;for (int i = 0; i < len; i++) {if (nums[i] <= mid) count++;} if (count > mid) ed = mid - 1; // 这里必须是>,因为可能为0或相等else st = mid + 1;}return st;}
};

leetcode287. Find the Duplicate Number相关推荐

  1. LeetCode 287. Find the Duplicate Number (时间复杂度O(n)) + 链表判断环

    LeetCode 287. Find the Duplicate Number 暴力解法 时间 O(nlog(n)),空间O(n),按题目中Note"只用O(1)的空间",照理是过 ...

  2. LeetCode 287. Find the Duplicate Number

    题目: Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), ...

  3. LeetCode.287 Find the Duplicate Number

    题目: Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), ...

  4. LintCode Find the Duplicate Number

    Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...

  5. LeetCode 287---Find the Duplicate Number

    问题链接:LeetCode 287-Find the Duplicate Number 题目大意 : 找出序列中唯一一个重复出现的数字,且只能使用o(1)的额外空间 实现代码如下: public cl ...

  6. 287. **Find the Duplicate Number

    287. **Find the Duplicate Number https://leetcode.com/problems/find-the-duplicate-number/description ...

  7. D19:Duplicate Number(重复数字,翻译+题解)

    原题:OpenJudge - 19:Duplicate Number 翻译: 描述:给定一个N个数的序列,求一个在序列中的至少出现2次的数A: 输入:第一行:一个不大于1000的正整数N : 第二行: ...

  8. leetcode 287. Find the Duplicate Number | 287. 寻找重复数(判断链表是否有环,并找到环的起点)

    题目 https://leetcode.com/problems/find-the-duplicate-number/ 题解 题目有限制 不能修改数组元素,必须 O(1) 空间复杂度,所以 不能排序, ...

  9. 633 - Find the Duplicate Number

    2017.9.11 感觉并没有啥意思啊这题,还是我误解了这题的真谛? public class Solution {/*** @param nums an array containing n + 1 ...

最新文章

  1. Mac下Sublime text2中文乱码问题的解决
  2. 读书笔记2014年第1本:《赤裸裸的统计学》
  3. 自动生成优化的Java类专业知识
  4. java创建两个foo方法_Java类实例化原理 - osc_foo7glsg的个人空间 - OSCHINA - 中文开源技术交流社区...
  5. JDBC连接MySQL数据库:建立SSL连接出现警告信息
  6. iOS 14惊现iPhone 12 Pro设计图细节:大失所望!
  7. Linux下自动备份Oracle数据库并删除指定天数前的备份
  8. AAAI、IJCAI和ACL录用三名清华本科生成果,华人NLP最杰出HowNet成功融入DL模型
  9. java常用设计模式详解及应用
  10. GD32与STM32区别
  11. 矩阵乘法 c/c++代码
  12. WebM视频解析与修复
  13. 记录python量化投资学习过程(二)- 常见指标以及概念的记录
  14. 女程序员南漂3年:有种心酸叫孤单的人总是晚回家
  15. oracle误删除一条数据库,Oracle误删除数据的恢复方法
  16. bga封装扇出过孔_pads router“BGA封装扇出”
  17. Oracle查询某个日期的周一到周日SQL语句
  18. Apache POI Excel、WORD、PowerPoint、Visio处理
  19. 可生成高清视频的Stable Diffusion来了!分辨率提升4倍,超分算法来自腾讯,支持Colab在线试玩...
  20. (已更新)社区论坛小程序源码,功能齐全,简洁漂亮,前端+后端

热门文章

  1. Effective C++ 的52个条款列表
  2. Testing AJAX Applications with VSTS 2008
  3. 关于nginx rewrtie的四种flag
  4. Oracle查看用户权限
  5. RIPv1和v2综合实验(CCNP阶段)
  6. 还有王法吗?美国联邦调查局开发人脸识别系统,数据源涉及公民隐私
  7. oracle存储过程、声明变量、for循环(转)
  8. 首尔日记——5月25日,出发
  9. java从入门到精髓 - 反射Constructor
  10. shodan 渗透测试 漏洞挖掘 一些用法