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:

  1. You must not modify the array (assume the array is read only).  //不能排序
  2. You must use only constant, O(1) extra space.                         // 不能用哈希表
  3. Your runtime complexity should be less than O(n2).                  //不能暴力求解
  4. There is only one duplicate number in the array, but it could be repeated more than once.

https://segmentfault.com/a/1190000003817671#articleHeader4

考虑:

  1. 暴力求解,选择一个数,看有没有重复的;
  2. 哈希表
  3. 排序后遍历
  4. 二分法
  5. 设置快慢指针,映射找环法
 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的更多相关文章

  1. 287. Find the Duplicate Number 找出数组中的重复数字

    [抄题]: Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive ...

  2. Leedcode算法专题训练(数组与矩阵)

    1. 把数组中的 0 移到末尾 283. Move Zeroes (Easy) Leetcode / 力扣 class Solution { public void moveZeroes(int[] ...

  3. [LeetCode] Find the Duplicate Number 寻找重复数

    Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...

  4. 287. Find the Duplicate Number hard

    287. Find the Duplicate Number   hard http://www.cnblogs.com/grandyang/p/4843654.html 51. N-Queens h ...

  5. Find the Duplicate Number

    Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...

  6. LeetCode——Find the Duplicate Number

    Description: Given an array nums containing n + 1 integers where each integer is between 1 and n (in ...

  7. 287. Find the Duplicate Number

    Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...

  8. [LeetCode] 287. Find the Duplicate Number 解题思路

    Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...

  9. Find the Duplicate Number 解答

    Question Given an array nums containing n + 1 integers where each integer is between 1 and n (inclus ...

随机推荐

  1. 优化浏览器默认scroll样式小技巧

    一个最简单的页面: <!DOCTYPE html> <html> <head> <title>优化scroll</title> <me ...

  2. C# 注册Dll文件

    有时会遇到dll在系统中不存在,需要程序自己去注册所需的dll文件. 注册dll 需要用到regsvr32命令,其用法为:"regsvr32 [/s] [/n] [/u] [/i[:cmdl ...

  3. WCF 客户端连接慢

    WCF客户端第一次连接超过1分钟,以后再连接就快了. 在 Config中加入 <basicHttpBinding> <binding name="BasicHttpBind ...

  4. python学习之路---day008

    文件操作一:文件操作01):文件读取:(r 只读) 001):我们先在当前文件夹内创建txt文件取名为123,在其中添加几句话内容. f 称之为文件句柄,控制着 123 这个文本文档 f=open(& ...

  5. CDQZ Day1

    #include<cassert> #include<cstdio> #include<vector> using namespace std; ,maxt=,ma ...

  6. 洛谷 P3244 / loj 2115 [HNOI2015] 落忆枫音 题解【拓扑排序】【组合】【逆元】

    组合计数的一道好题.什么非主流题目 题目背景 (背景冗长请到题目页面查看) 题目描述 不妨假设枫叶上有 \(n​\) 个穴位,穴位的编号为 \(1\sim n​\).有若干条有向的脉络连接着这些穴位. ...

  7. BZOJ - 2115 独立回路 线性基

    题意:给定一个图集\((V,E)\),求路径\(1...n\)的最大异或和,其中重复经过的部分也会重复异或 所求既任意一条\(1...n\)的路径的异或和,再异或上任意独立回路的组合的异或和(仔细想想 ...

  8. POJ - 3735 循环操作

    构造n+1元组,m次方的矩阵代表循环操作 本题尚有质疑之处(清零操作的正确性还有单位矩阵的必要性),题解可能会改正 #include<iostream> #include<algor ...

  9. UESTC - 618

    #include<bits/stdc++.h> using namespace std; const int maxn = 1e6+11; const int N = 1e6; typed ...

  10. A - TOYS(POJ - 2318) 计算几何的一道基础题

    Calculate the number of toys that land in each bin of a partitioned toy box. 计算每一个玩具箱里面玩具的数量 Mom and ...