数组和矩阵(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 ...
随机推荐
- 优化浏览器默认scroll样式小技巧
一个最简单的页面: <!DOCTYPE html> <html> <head> <title>优化scroll</title> <me ...
- C# 注册Dll文件
有时会遇到dll在系统中不存在,需要程序自己去注册所需的dll文件. 注册dll 需要用到regsvr32命令,其用法为:"regsvr32 [/s] [/n] [/u] [/i[:cmdl ...
- WCF 客户端连接慢
WCF客户端第一次连接超过1分钟,以后再连接就快了. 在 Config中加入 <basicHttpBinding> <binding name="BasicHttpBind ...
- python学习之路---day008
文件操作一:文件操作01):文件读取:(r 只读) 001):我们先在当前文件夹内创建txt文件取名为123,在其中添加几句话内容. f 称之为文件句柄,控制着 123 这个文本文档 f=open(& ...
- CDQZ Day1
#include<cassert> #include<cstdio> #include<vector> using namespace std; ,maxt=,ma ...
- 洛谷 P3244 / loj 2115 [HNOI2015] 落忆枫音 题解【拓扑排序】【组合】【逆元】
组合计数的一道好题.什么非主流题目 题目背景 (背景冗长请到题目页面查看) 题目描述 不妨假设枫叶上有 \(n\) 个穴位,穴位的编号为 \(1\sim n\).有若干条有向的脉络连接着这些穴位. ...
- BZOJ - 2115 独立回路 线性基
题意:给定一个图集\((V,E)\),求路径\(1...n\)的最大异或和,其中重复经过的部分也会重复异或 所求既任意一条\(1...n\)的路径的异或和,再异或上任意独立回路的组合的异或和(仔细想想 ...
- POJ - 3735 循环操作
构造n+1元组,m次方的矩阵代表循环操作 本题尚有质疑之处(清零操作的正确性还有单位矩阵的必要性),题解可能会改正 #include<iostream> #include<algor ...
- UESTC - 618
#include<bits/stdc++.h> using namespace std; const int maxn = 1e6+11; const int N = 1e6; typed ...
- A - TOYS(POJ - 2318) 计算几何的一道基础题
Calculate the number of toys that land in each bin of a partitioned toy box. 计算每一个玩具箱里面玩具的数量 Mom and ...