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.
Example 1:
Input:[1,3,4,2,2]
Output: 2
Example 2:
Input: [3,1,3,4,2]
Output: 3
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
以为要用位运算,结果是:出现两次的value当作指针,数组形成环。所以用快慢指针
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[一句话思路]:
数组中的一步两步:value变成index,用数组包起来
一层/两层:
slow = nums[slow]; fast = nums[nums[fast]];
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 不知道为什么要从头再走一遍:直接找的中点是从中间断开的。为了从起点开始,需要再走一遍。
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
直接找的中点是从中间断开的。为了从起点开始,需要再走一遍。
[复杂度]:Time complexity: O(方) Space complexity: O(1)
[算法思想:迭代/递归/分治/贪心]:
[关键模板化代码]:
//start again and move to slow
fast = 0;
while (fast != slow) {
slow = nums[slow];
fast = nums[fast];
}
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
[潜台词] :
class Solution {
public int findDuplicate(int[] nums) {
//corner case
if (nums == null || nums.length == 0) return -1;
//initialization: slow, fast
int slow = nums[0];
int fast = nums[nums[0]];
//find the number and store in slow
while (fast != slow) {
slow = nums[slow];
fast = nums[nums[fast]];
}
//store in slow now
//start again and move to slow
fast = 0;
while (fast != slow) {
slow = nums[slow];
fast = nums[fast];
}
//return
return fast;
}
}
287. Find the Duplicate Number 找出数组中的重复数字的更多相关文章
- 【剑指offer】找出数组中任意重复的数字(不修改数组),C++实现
原创博文,转载请注明出处! # 题目 在一个长度为n+1的数组里的所有数字都在1~n的范围内,所以数组中至少有一个数字是重复的.请找出数组中任意一个重复的数字,但不能修改输入的数组.例如,如果输入长度 ...
- [LeetCode] Find All Duplicates in an Array 找出数组中所有重复项
Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others ...
- 剑指offer28:找出数组中超过一半的数字。
1 题目描述 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组中出现了5次,超过数组长度的一半,因此输出 ...
- 442. Find All Duplicates in an Array找出数组中所有重复了两次的元素
[抄题]: Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and o ...
- 找出数组中特定和数字下标(JAVA)
比如: 输入: numbers={2, 7, 11, 15}, target=9 输出: index1=1, index2=2 public class _003TwoSum { public sta ...
- 1. 找出数组中的单身狗OddOccurrencesInArray Find value that occurs in odd number of elements.
找出数组中的单身狗: 1. OddOccurrencesInArray Find value that occurs in odd number of elements. A non-empty ze ...
- 【Java】 剑指offer(1) 找出数组中重复的数字
本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集 题目 在一个长度为n的数组里的所有数字都在0到n-1的范围内.数组中某些数字 ...
- 《剑指offer》第三_一题(找出数组中重复的数字,可改变数组)
// 面试题3(一):找出数组中重复的数字 // 题目:在一个长度为n的数组里的所有数字都在0到n-1的范围内.数组中某些数字是重复的,但不知道有几个数字重复了, // 也不知道每个数字重复了几次.请 ...
- 【Offer】[3-1] 【找出数组中重复的数字】
题目描述 思路 Java代码 代码链接 题目描述 在一个长度为n的数组里的所有数字都在0~n-1的范围内.数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次. 请找出数组中任 ...
随机推荐
- SQL SERVER 月、季、年统计与常用查询语句汇总
一.SQL SERVER 月.季.年统计查询 --本天 SELECT *FROM dbo.TableName WHERE DATEDIFF(DAY,TimeField,getdate())= 0; - ...
- Mycat 镜像-创建 Docker 镜像
将 Mycat-server 创建到镜像,使其能够进行容器化部署,我们需要创建 Dockerfile 并在文件中安装其依赖项,使用 centos 做为 base 镜像,并安装 jdk 依赖即可,因此创 ...
- C 栈实现队列节点的管理
栈预先存储节点,队列的malloc/free均有栈模拟,从而保证不频繁的开辟/是否节点内存. #include "com_is_buf.h" #include "com_ ...
- Ubuntu 14.10 下DokuWiki安装
环境说明: Ubuntu 14.10 64位 1 下载DokuWiki:http://download.dokuwiki.org/ 2 解压到 /var/www/html下面 3 如果没有安装Apac ...
- 基础 - 字符读取函数scanf、getchar、gets、cin(清空缓存区解决单字符回车问题)
0x01 scanf.getchar.cin读取单字符: 如下: //scanf读取字符 回车问题 void Sub_1_1() { char v1,v2; scanf("%c", ...
- json null
{ "ResourceId": 0, "JsonKey": "Account", "GroupId": 21, &quo ...
- Vue2.0学习笔记
环境搭建 vue-cli@3 vue-cli@2.X npm i -g @vue/cli 模板语法 文本 <span>Message: {{ msg }}</span> ...
- SQLServer数据库镜像配置
目录 一.目标...2 二.前提条件.限制和建议...2 三.设置概述...2 四.安装Sql Server 2008 enterprise X64.3 4.1.安装.NET3.5.3 4.2.安装时 ...
- 史上最全 40 道 Dubbo 面试题及答案,看完碾压面试官
想往高处走,怎么能不懂 Dubbo? Dubbo是国内最出名的分布式服务框架,也是 Java 程序员必备的必会的框架之一.Dubbo 更是中高级面试过程中经常会问的技术,无论你是否用过,你都必须熟悉. ...
- profile default1
DEVPISAP01:/sapmnt/ISD/profile # more ISD_J20_SHADEVEAIAP01 SAPSYSTEMNAME = ISD SAPSYSTEM = 20 INSTA ...