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. Java面向对象之内部类(访问格式)

    一.基础概念 1.内部类是定义在类中的类.A类需要直接访问B类中的成员时,可以将A类定义到B类中,作为B类的内部类存在. 2.内部类可以相当于外部类中的一个成员,可以被成员修饰符所修饰.(如:publ ...

  2. LAMP课程

    LAMP课程 上次课回顾: ls -a:查看全部目录内容 若文件名以“.”开头,则认为是隐藏的文件. ls-l:可以直接用命令 ll命令:ls -l 的别名. ls -m:横向显示文件和目录 ls - ...

  3. python模块-hmac

    Hmac算法:Keyed-Hashing for Message Authentication.它通过一个标准算法,在计算哈希的过程中,把key混入计算过程中. import timefrom has ...

  4. 快速排序(一) 思想 JAVA实现

    已知数组59.71.37.56.88.96.21.58.48.43 采用快速排序将数组有序. 快速排序同样采用了“分治策略”,使用递归的思路来实现算法. 快速排序的算法思想: 9.71.37.56.8 ...

  5. C++ #include " " 与 <>有什么区别?

    #include <> 和 #include "" 都会在实现定义的位置查找文件,并将其包含. 区别是若 #include "" 查找成功,则遮蔽 ...

  6. 黑马学习CSS之CSS模块化规范全部组成 CSS属性列表

  7. excel文件的groovy脚本在SoapUI中进行数据驱动测试

    SoapUI Pro具有从外部文件读取数据的功能,例如:excel,csv等.但SoapUI不提供从excel文件读取数据的功能.因此,为了从SoapUI中的excel文件中读取数据,我们需要在gro ...

  8. mysql 查询库中有几张表

    SELECT COUNT(*) TABLES, table_schemaFROM information_schema.TABLESWHERE table_schema = '999*999' 999 ...

  9. 【算法笔记】B1043 输出PATest

    1043 输出PATest (20 分) 给定一个长度不超过 10​4​​ 的.仅由英文字母构成的字符串.请将字符重新调整顺序,按 PATestPATest.... 这样的顺序输出,并忽略其它字符.当 ...

  10. 113th LeetCode Weekly Contest Reveal Cards In Increasing Order

    In a deck of cards, every card has a unique integer.  You can order the deck in any order you want. ...