zoukankan      html  css  js  c++  java
  • [LeetCode#268]Missing Number

    Problem:

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

    For example,
    Given nums = [0, 1, 3] return 2.

    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.

    Analysis:

    This kind of problem is very typical and quite tricky. 
    Condition: the numbers in the array are distinct, and all of them fall into the range [0, n], and the array's size is n.
    This condition means only one element would have no palce to reside in.
    
    Tricky skill:
    Scan from the first element, try to place all elements interwining with the number in the first element. 
    for (int i = 0; i < nums.length; i++) {
        while (nums[i] != i && nums[i] < nums.length) {
            int temp = nums[i];
            nums[i] = nums[nums[i]];
            nums[temp] = temp;
        }
    }
    Unless the nums[i] = nums.length, we could always find the right position for nums[i].
    Two stop cases:
    a. nums[i] == i, which means nums[i] was placed at the right position.
    b. nums[i] == nums.length, which means we could find no place to place the nums[i], otherwise exception appears. 
    
    During the whole process, we place nums[i] to the right position, and exchange the nums[nums[i]] back to continue the placement process. 
    
    After we finish the scan over the whole string, all placable elements were placed at their right positions. The unplacement element(if exist) was placed at the missing element's position. 
    
    Two cases for answer:
    1. the unplacement element was placed at the missing element's position.
    for (int j = 0; j < nums.length; j++) {
        if (nums[j] != j)
            return j;
    }
    2. all elements are placable, and they were placed at their right position. 
    return nums.length;
    
    
    
    Errors:
    while (nums[i] != i && i < nums.length) {
        int temp = nums[i];
        nums[i] = nums[nums[i]];
        nums[nums[i]] = temp;
    }
    The above code snippt could cause infinite loop.
    Case [1, 0]
    i = 0
    temp = nums[0] (1);
    nums[0] = nums[nums[0](1)] (0);
    nums[nums[0]0] = temp;(1)
    Do you see the loop?
    Even the logic in swapping is right, but the index<nums[i]> we use was changed in the middle!!!
    nums[i] was changed at: nums[i] = nums[nums[i]];
    This is very special, a way to fix it is to record nums[i]'s value
    while (nums[i] != i && nums[i] < nums.length) {
        int temp = nums[i];
        nums[i] = nums[nums[i]];
        nums[temp] = temp;
    }

    Solution:

    public class Solution {
        public int missingNumber(int[] nums) {
            if (nums == null || nums.length == 0)
                return 0;
            for (int i = 0; i < nums.length; i++) {
                while (nums[i] != i && nums[i] < nums.length) {
                    int temp = nums[i];
                    nums[i] = nums[nums[i]];
                    nums[temp] = temp;
                }
            }
            for (int j = 0; j < nums.length; j++) {
                if (nums[j] != j)
                    return j;
            }
            return nums.length;
        }
    }
  • 相关阅读:
    python利用scapy模块写一个TCP路由追踪和扫描存活IP的脚本
    mitm6:通过IPv6攻破IPv4网络
    php写一个判断是否有cookie的脚本
    python写一个DDos脚本(DOS)
    分布式系统设计系列 -- 基本原理及高可用策略(转)
    在Ubuntu 16.04 上编译安装OpenCV3.2.0(Cmake + python3 + OpenCV3)(转)
    好玩的Raft动画演示,原理秒懂
    全球分布式数据库:Google Spanner(论文翻译)
    分布式系统设计系列 -- 基本原理及高可用策略 (转)
    MySQL更新优化(转)
  • 原文地址:https://www.cnblogs.com/airwindow/p/4759179.html
Copyright © 2011-2022 走看看