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的范围内.数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次. 请找出数组中任 ...
随机推荐
- GoStudy——解决vscode中golang插件依赖安装失败问题
vscode中安装ms-vscode.go插件后可以开启对go语言的支持,ms-vscode.go插件需要依赖一些工具,安装完成后提示 Installing github.com/nsf/gocode ...
- 让所有浏览器支持HTML5 video视频标签
HTML5究竟需要多少种视频编码格式 当前,video 元素支持三种视频格式:Ogg = 带有 Theora 视频编码和 Vorbis 音频编码的 Ogg 文件MPEG4 = 带有 H.264 视频编 ...
- requests库(爬虫)
北京理工大学嵩天老师的课程:http://www.icourse163.org/course/BIT-1001870001 官方文档:http://docs.python-requests.org/e ...
- 【mysql】mysql触发器使用示例
mysql触发器 时间点:before/after 触发事件: update/delete/insert 时间点+触发事件:构成一个完整的触发器的触发时机: 一个触发时机最多只能由1个Trigger: ...
- ss客户端以及tcp,udp,dns代理ss-tproxy在线安装版--centos7.3 x64以上(7.3-7.6x64测试通过)
#!/bin/sh # # Script for automatic setup of an SS-TPROXY server on CentOS 7.3 Minimal. # export PATH ...
- vue+窗格切换+田字+dicom显示_02
环境:vue+webpack+cornerstone ide:vs code 需求:窗格设置+拼图设置 分析: 由于时间的原因,我也没有Baidu更好的显示窗格的方法,所以使用比较笨的方法,通过组件显 ...
- git 第一次提交代码
git init git add README.md git commit -m "first commit" git remote add origin https://git. ...
- tomcat JRE_HOME
tomcat 不知道什么时候开始需要 JRE_HOME了: D:\java\apache-tomcat-7.0.68\bin>startup.batJAVA_HOME == D:\Progra ...
- 三. html&JavaScript&ajax 部 分
1. 判 断 第 二 个 日 期 比 第 一 个 日 期 大 如何用脚本判断用户输入的的字符串是下面的时间格式2004-11-21必须要保证用户 的输入是此格式,并且是时间,比如说月份不大于12等等, ...
- leetcode141
/** * Definition for singly-linked list. * public class ListNode { * public int val; * public ListNo ...