Blue Bridge Cup Algorithm Training the shortest circuit (chain front star, spfa)

2022-12-26   ES  

1, demand

  • A length of a length of N-1 is unique in the number of sorted sorting arrays, and each number is within 0 to N-1;

  • among the n numbers in the range 0 to N-1, and only one number is not in the array, please find this number;

two, two -point method

2.1 Analysis of ideas

  1. According to the sorting number, first think of the two -point method;
  2. First divide the original arrays into left and right array, and the left array meets Nums [i] = i, and the right array meets NUMS [i]! = I, where i starts from 0, so the missing number is the right of the right The index corresponding to the first element of the array;
  3. So now use dictators to find the first element of the right array, initialize the strategy. When num [mid] == m, it means that the element is still on the right, so that i = mid + 1, when nums [mid]! = M At this time, because it is not necessarily the first element of the right array, the j = MID -1 is allowed, so whether it is the first element, the last I will return;

2.2 code implementation

class solution { 
     Public int missingnumber (int [] nums) { 
         // The specified interval closed left and right closed 
         int i = 0; 
         int j = nums.length -1; 
         While (i <= j) { 
            // int m = i + (j -i) / 2; 
             int m = (i + j) / 2; 
             if (nums [m] == m) { 
                 i = m + 1; 
             } else { 
                 j = m -1; 
             } 
         } 
         Return i; 
     } 
 }

2.3 Analysis of complexity

  • Time complexity isO(log_2N), the complexity of the two -point method to the number level;
  • Space complexity isO(1), a few variables use extra space of constant size;

3. Learning address

Author: krahets

link:https://leetcode-cn.com/problems/que-shi-de-shu-zi-lcof/solution/mian-shi-ti-53-ii-0n-1zhong-que-shi-de-shu-zi-er-f/

source

Related Posts

System calls to implement Linux command CP

One -trick to control the greedy algorithm Androidslost

MyBatis-Plus Open and close SQL log printing

Color nominement Color-Name Dictionaries

Blue Bridge Cup Algorithm Training the shortest circuit (chain front star, spfa)

Random Posts

java adds tool prompts in PDF | Tooltip

One -trick to control the greedy algorithm Androidslost

[Angular1] Basic use and various methods

Hadoop MapReduce Case Word Count Local environment encountered some problems with Ichimarugin encountered when running local environment.

zhang-suen detailed algorithm explanation and implementation