数组和矩阵(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 ...
随机推荐
- python3入门之字符串
获得更多资料欢迎进入我的网站或者 csdn或者博客园 经过前面的介绍相信大家也对python有了一个初步的了解:本节主要介绍字符串,不管学习什么编语言字符串一定在其中扮演着重要的地位.本节主要讲解,字 ...
- ExtJS 4.2.1学习笔记(一)——MVC架构与布局
1 ExtJS入门 1.1 支持所有主流浏览器 调试推荐:chrome.Safari.Firefox 1.2 推荐目录结构 - appname (包含所有程序代码,是根目录 ...
- count distinct 组合使用
SELECT COUNT(DISTINCT Lbox_Sn) FROM Tab_History_Info
- CSL 的字符串(思维+STL操作)
链接:https://ac.nowcoder.com/acm/contest/551/D 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 524288K,其他语言1048 ...
- new Date("2018-01-01 11:11:11").valueOf() 在IE下会返回 NaN
原因是在ie下 new Date不能处理 小横线 这种时间格式,但是 替换成 斜线就可以正常获得毫秒数,像下面这样: new Date(('2018-01-01 11:11:11').replace( ...
- 【研究】Metasploit自动攻击模块
环境:kali-linux-2017.3-vm-amd64 一.安装postgresql数据库 apt-get install postgresql apt-get install rubygems ...
- 剑指offer——面试题6:从尾到头打印链表
#include"iostream" #include"stdio.h" #include"stack" using namespace s ...
- my31_MGR单写模式压测以及对比普通从库记录
场景MGR单写模式三节点,db46写节点,db47/db48为读节点工具sysbencn.压测15个小时,db46上18线程纯写,12线程oltp混合测试,db48上12线程select在压测2个小时 ...
- MongoChef
简介 开源且免费,有商业版 可自动化生成查询语句 使用 最下面的 _id 是自动生成的,手动指定 { .0, "_id" : ObjectId("58 ...
- 算法市场 Algorithmia
算法市场 官网:(需要***,fan qiang,不然可能访问不了或登录不了) https://algorithmia.com/ 官方的例子: 我不用 curl 发请求,把 curl 命令粘贴给你们用 ...