zoukankan      html  css  js  c++  java
  • [LeetCode] 268. Missing Number 缺失的数字

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.

    Example 1

    Input: [3,0,1]
    Output: 2

    Example 2

    Input: [9,6,4,2,3,5,7,0,1]
    Output: 8

    Note:
    Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?

    Credits:
    Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

    给n个数,0到n之间有一个数字缺失了,让找到这个缺失数字。要求线性时间复杂度和常数级的额外空间复杂度。

    解法1:排序,然后二分查找。此题数组不是排序的,所以适合。但面试时,如果问道要知道。

    解法2:求差值,用0~n个数的和减去给定数组数字的和,差值就是要找的数字。

    解法3:位操作Bit Manipulation,将这个数组与0~n之间完整的数异或一下累加到res,相同的数字异或后都变为了0,最后剩下的结果就是缺失的数字。

    Java:

    class Solution {
        public int missingNumber(int[] nums) {
            if (nums == null || nums.length == 0) return 0;
            int result = 0;
    
            Arrays.sort(nums);
            for (int i = 0; i < nums.length; i++) {
                if (result != nums[i]) {
                    return result;
                }
                result++;
            }
            return nums.length;
        }
    }  

    Java:

    class Solution {
        public int missingNumber(int[] nums) {
            int xor = 0, i = 0;
    
            for (i = 0; i < nums.length; i++) {
                xor = xor ^ i ^ nums[i];
            }
    
            return xor ^ i;
        }
    }  

    Python:

    def missingNumber(self, nums):
        n = len(nums)
        return n * (n+1) / 2 - sum(nums)  

    Python:

    class Solution(object):
        def missingNumber(self, nums):
            return sum(xrange(len(nums)+1)) - sum(nums)
    

    Python:

    class Solution(object):
        def missingNumber(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            return reduce(operator.xor, nums, 
                          reduce(operator.xor, xrange(len(nums) + 1)))  

    C++: 用等差数列公式

    class Solution {
    public:
        int missingNumber(vector<int>& nums) {
            int sum = 0, n = nums.size();
            for (auto &a : nums) {
                sum += a;
            }
            return 0.5 * n * (n + 1) - sum;
        }
    };
    

    C++:

    class Solution {
    public:
        int missingNumber(vector<int>& nums) {
            int res = 0;
            for (int i = 0; i < nums.size(); ++i) {
                res ^= (i + 1) ^ nums[i];
            }
            return res;
        }
    };

    类似题目:

    [CareerCup] 5.7 Find Missing Integer 查找丢失的数

    All LeetCode Questions List 题目汇总

  • 相关阅读:
    android中fragment的获取与隐藏
    Android Toolbar教程,Android标题栏经典设计教程
    Android6.0以后,在BroadcastReceiver中启动AlertDialog导致程序停止运行的问题及解决办法
    java数据结构和算法--------第六章
    动态规划--------0和1背包问题
    动态规划------平均切分数组之和为两部分
    java数据结构和算法-----第四章
    java数据结构和算法-------第三章
    java数据结构和算法----第二章
    动态规划----Ones and Zeroes
  • 原文地址:https://www.cnblogs.com/lightwindy/p/8656749.html
Copyright © 2011-2022 走看看