数组和矩阵(1)——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.
https://segmentfault.com/a/1190000003817671#articleHeader4
考虑:
- 暴力求解,选择一个数,看有没有重复的;
- 哈希表
- 排序后遍历
- 二分法
- 设置快慢指针,映射找环法
public class Solution {
public int findDuplicate(int[] nums) { //映射找环
int n = nums.length - 1;
int pre = 0;
int last = 0;
do {
pre = nums[pre];
last = nums[nums[last]];
} while(nums[pre] != nums[last]);
last = 0;
while(nums[pre] != nums[last]) {
pre = nums[pre];
last = nums[last];
}
return nums[last];
}
}
数组和矩阵(1)——Find the Duplicate Number的更多相关文章
- 287. Find the Duplicate Number 找出数组中的重复数字
[抄题]: Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive ...
- Leedcode算法专题训练(数组与矩阵)
1. 把数组中的 0 移到末尾 283. Move Zeroes (Easy) Leetcode / 力扣 class Solution { public void moveZeroes(int[] ...
- [LeetCode] Find the Duplicate Number 寻找重复数
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...
- 287. Find the Duplicate Number hard
287. Find the Duplicate Number hard http://www.cnblogs.com/grandyang/p/4843654.html 51. N-Queens h ...
- Find the Duplicate Number
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...
- LeetCode——Find the Duplicate Number
Description: Given an array nums containing n + 1 integers where each integer is between 1 and n (in ...
- 287. Find the Duplicate Number
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...
- [LeetCode] 287. Find the Duplicate Number 解题思路
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...
- Find the Duplicate Number 解答
Question Given an array nums containing n + 1 integers where each integer is between 1 and n (inclus ...
随机推荐
- 题解 P1720 【月落乌啼算钱】
题目链接 定义一个函数比较好求. #include<bits/stdc++.h>//万能头文件 using namespace std; double F(int x)//定义函数,为了保 ...
- struts2配置文件中的method={1}详解
struts.xml中的配置: <!-- 配置用户模块的action --> <action name="user_*" class="userActi ...
- 使用原生实现jquery中的css方法
由于jquery放在mobile页面上,有时候还是显得有点大,所以今天尝试使用原生来开发,但是习惯了jquery之后,转用原生开发之后,发现原生中,找不到可以替代jquery的css方法,于是对原生的 ...
- redis incr自增指定的无限期 key 删除问题
不得不说,最近在使用redis的incr的功能的时候,着实是踩了不少的坑啊! 前面我写的一篇文章里面,我提到通过incr产生的键在用普通的get的方式去获取的时候,是获取不到的,所以我不得不通过特殊的 ...
- GI缓存
GI缓存 设置 属性 最大高速缓存大小(GB) 使用滑块设置最大GI缓存文件夹大小.只要可能,GI缓存文件夹将保持在此大小以下.定期删除未使用的文件以创建更多空间.这是由编辑器自动执行,不需要你做任何 ...
- python学习之路---day23--模块
模块基本小结if __name__ == '__main__':一:import 引入模块模块:是一个包含python定义和声明的文件,文件名就是模块名字加上.py后缀,所有的py文件都可以看成是一个 ...
- mysql 导入 导出
mysql导入导出sql文件 window下 1.导出整个数据库mysqldump -u 用户名 -p 数据库名 > 导出的文件名mysqldump -u dbuser -p dbname ...
- C++_基础5-内存模型
C++为在内存中存储数据提供了多种选择: 可以选择数据保留在内存中的时间长度(存储持续性): 程序的哪一部分可以访问数据(作用域和链接): 可以使用new来动态地分配内存:定位new运算符提供了这种技 ...
- 解决页面使用overflow: scroll,overflow-y:hidden在iOS上滑动卡顿的问题
解决页面使用overflow: scroll,overflow-y:hidden在iOS上滑动卡顿的问题 div{ width: 100%; overflow-y: hidden; -webkit-o ...
- pycharm 安装tushare
1.教程非常简单,但是我确研究了整整一个晚上,分享下经历 2.安装tushare包的时候,先要安装5个依赖包 lxml,beautifulsoup4,pandas,requests,simplejso ...