zoukankan      html  css  js  c++  java
  • LF68.Missing Number I

    Given an integer array of size N - 1, containing all the numbers from 1 to N except one, find the missing number.

    Assumptions

    The given array is not null, and N >= 1
    Examples

    A = {2, 1, 4}, the missing number is 3
    A = {1, 2, 3}, the missing number is 4
    A = {}, the missing number is 1

     
     1 //time o(n) space o(n)
     2     public int missing_method1(int[] array) {
     3         // Write your solution here
     4         //corner case
     5         if (array == null){
     6             return -1 ;
     7         }
     8         // since we know for sure there is one number missing from array
     9         int size = array.length ;
    10         //1: put for 1 to size into hashset
    11         Set<Integer> dic = new HashSet<>(size) ;
    12         for (int i = 0 ; i <size ; i++){
    13             dic.add(array[i]);
    14         }
    15         //2: and then loop through the array and cross check with the dic
    16         for (int i = 1; i <= size +1 ; i++) {
    17             if (!dic.contains(i)){
    18                 return i;
    19             }
    20         }
    21         return -1 ;
    22     }
    23 
    24     // time o(n) space o(1)
    25     //use sum
    26     public int missing_method2(int[] array) {
    27         // Write your solution here
    28         //corner case
    29         if (array == null){
    30             return -1 ;
    31         }
    32         int n = array.length +1  ;
    33         long targetSum = (n +0L) * (n+1)/2 ;
    34         long actualSum = 0L ;
    35         for(int num : array){
    36             actualSum += num ;
    37         }
    38         return (int)(targetSum - actualSum);
    39     }
    40 
    41     //xor bit operation - time o(n) sapce: o(1)
    42     public int missing_method3(int[] array) {
    43         // Write your solution here
    44         //corner case
    45         if (array == null){
    46             return -1 ;
    47         }
    48         int n = array.length  + 1 ;
    49         int xor = 0;
    50         //xor from 1 to n
    51         for (int i = 1; i <= n; i++) {
    52             xor ^= i ;
    53         }
    54         //then iterate all the items in array
    55         /*
    56         after this operation, all the numbers from 1 to n
    57         are pair xor'ed except for the missing number.
    58         since x ^ x = 0, the remaining number is the result
    59         */
    60         for(int num : array){
    61             xor ^= num ;
    62         }
    63         return xor ;
    64     }
    
    

     

    
    
  • 相关阅读:
    php 信号量
    .net 反射初体验
    IEnumerable,IQueryable之前世今生
    [SQL]511+512+534+550+569
    [SQL]183+184+185+196+197
    [SQL]3.26--175+176+177+178+180+181+182
    [剑指offer]10.斐波那契数列+青蛙跳台阶问题
    [剑指offer]14-1.剪绳子
    [剑指offer]62.圆圈中最后剩下的数字
    [剑指offer]52.两个链表的第一个公共节点
  • 原文地址:https://www.cnblogs.com/davidnyc/p/8721080.html
Copyright © 2011-2022 走看看