# 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 is$O(log_2N)$, the complexity of the two -point method to the number level;
• Space complexity is$O(1)$, a few variables use extra space of constant size;

Author: krahets

source