|
| 1 | +package me.meet.leetcode.medium; |
| 2 | + |
| 3 | +public final class MissingNumber { |
| 4 | + private MissingNumber() {} |
| 5 | + |
| 6 | + /** |
| 7 | + * Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array. |
| 8 | + * |
| 9 | + * For example, |
| 10 | + * Given nums = [0, 1, 3] return 2. |
| 11 | + * |
| 12 | + * Note: |
| 13 | + * Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity? |
| 14 | + * |
| 15 | + * |
| 16 | + * 题意: |
| 17 | + * 这道题给我们n个数字,是0到n之间的数但是有一个数字去掉了,让我们寻找这个数字,要求线性的时间复杂度和常数级的空间复杂度。 |
| 18 | + * |
| 19 | + * 那么最直观的一个方法是用等差数列的求和公式求出0到n之间所有的数字之和,然后再遍历数组算出给定数字的累积和,然后做减法,差值就是丢失的那个数字 |
| 20 | + */ |
| 21 | + static int getMissingNumber(int[] arr) { |
| 22 | + int sum = 0, n = arr.length; |
| 23 | + for (int i = 0; i < n; i++) { |
| 24 | + sum += arr[i]; |
| 25 | + } |
| 26 | + |
| 27 | + return n*(n+1)/2 - sum; |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * 这题还有一种解法,使用位操作Bit Manipulation来解的,用到了异或操作的特性 |
| 32 | + * 那么思路是既然0到n之间少了一个数,我们将这个少了一个数的数组合0到n之间完整的数组异或一下,那么相同的数字都变为0了,剩下的就是少了的那个数字了 |
| 33 | + */ |
| 34 | + static int getMissingNumberXOR(int[] arr) { |
| 35 | + int res = 0, n = arr.length; |
| 36 | + for (int i = 0; i < n; i++) { |
| 37 | + res ^= (i+1)^arr[i]; |
| 38 | + } |
| 39 | + return res; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * 1. 排序 |
| 44 | + * 2. 二分查找到 丢失到数字 |
| 45 | + */ |
| 46 | + |
| 47 | + private static void testGetMissingNumber() { |
| 48 | + int[] arr = new int[]{0,1,2,3,4,6,7,8,9}; |
| 49 | + |
| 50 | + System.out.println(getMissingNumber(arr)); |
| 51 | + System.out.println(getMissingNumberXOR(arr)); |
| 52 | + } |
| 53 | + |
| 54 | + public static void main(String[] args) { |
| 55 | + testGetMissingNumber(); |
| 56 | + } |
| 57 | +} |
0 commit comments