当前位置:网站首页>[leetcode daily question] a single element in an ordered array
[leetcode daily question] a single element in an ordered array
2022-07-04 01:40:00 【Oysters bring the sea to Chicago】
A single element in an ordered array
difficulty : secondary 
The time complexity required for the topic is log n, It's not hard to think of solving it through binary search , The observation topic is easy to come to , Only once Subscripts are even .
The left bound of binary search is 0, The right boundary is the maximum subscript of the array . Take the average value of the left and right boundaries each time mid As a subscript to be judged , according to mid The parity of determines the comparison with the adjacent elements on the left or right :
- If mid It's even , Then compare nums[mid] and nums[mid+1] Whether it is equal or not ;
- If mid Is odd , Then compare nums[mid−1] and nums[mid] Whether it is equal or not .
If the result of the above comparison of adjacent elements is equal , be mid<x, Adjust the left border , otherwise mid≥x, Adjust the right border . After adjusting the boundary, continue the binary search , Until the subscript is determined x Value .
Get the subscript x After the value of ,nums[x] That is, the element that appears only once .
The code is as follows :
public int singleNonDuplicate(int[] nums) {
int left = 0;
int right = nums.length-1;
if(nums.length==1||nums[0]!=nums[1]){
return nums[0];
}
while (left<right){
int mid = (left+right)/2;
if (mid%2==0){
if (nums[mid]==nums[mid+1]){
left=mid+1;
}else if (nums[mid]==nums[mid-1]){
right = mid-1;
}else{
return nums[mid];
}
}else{
if (nums[mid]==nums[mid-1]){
left=mid+1;
}else if (nums[mid]==nums[mid+1]){
right = mid-1;
}else{
return nums[mid];
}
}
}
return nums[left];
}
Execution results : success 
边栏推荐
- Jerry's synchronous weather information to equipment [chapter]
- 【.NET+MQTT】. Net6 environment to achieve mqtt communication, as well as bilateral message subscription and publishing code demonstration of server and client
- Sequence sorting of basic exercises of test questions
- Applet graduation project based on wechat selection voting applet graduation project opening report function reference
- Avoid playing with super high conversion rate in material minefields
- Mobile phone battery - current market situation and future development trend
- AI helps make new breakthroughs in art design plagiarism retrieval! Professor Liu Fang's team paper was employed by ACM mm, a multimedia top-level conference
- Audio resource settings for U3D resource management
- Feign implements dynamic URL
- 2022 electrician (elementary) examination question bank and electrician (elementary) simulation examination question bank
猜你喜欢

Douban scoring applet Part-3
![Jerry's synchronous weather information to equipment [chapter]](/img/a9/e09bbf62161ea8ba60b0fce3fb5f89.jpg)
Jerry's synchronous weather information to equipment [chapter]

Avoid playing with super high conversion rate in material minefields

Rearrangement of tag number of cadence OrCAD components and sequence number of schematic page

MySQL - use of aggregate functions and group by groups

Feign implements dynamic URL

AI helps make new breakthroughs in art design plagiarism retrieval! Professor Liu Fang's team paper was employed by ACM mm, a multimedia top-level conference

Remember a lazy query error
![Cesiumjs 2022^ source code interpretation [8] - resource encapsulation and multithreading](/img/d2/99932660298b4a4cddd7e5e69faca1.png)
Cesiumjs 2022^ source code interpretation [8] - resource encapsulation and multithreading
![Jerry's modification setting status [chapter]](/img/23/d6eb521943b35e543a9681a98ad3be.jpg)
Jerry's modification setting status [chapter]
随机推荐
C import Xls data method summary I (upload files and create Workbooks)
Force buckle day32
技术实践|线上故障分析及解决方法(上)
Query efficiency increased by 10 times! Three optimization schemes to help you solve the deep paging problem of MySQL
File contains vulnerability summary
Solution of cursor thickening
Portable two-way radio equipment - current market situation and future development trend
Huawei BFD and NQA
Why is the operation unsuccessful (unresolved) uncaught syntaxerror: invalid or unexpected token (resolved)
Who moved my code!
G3 boiler water treatment registration examination and G3 boiler water treatment theory examination in 2022
Rearrangement of tag number of cadence OrCAD components and sequence number of schematic page
SQL statement
Hash table, string hash (special KMP)
机器学习基础:用 Lasso 做特征选择
Long article review: entropy, free energy, symmetry and dynamics in the brain
A little understanding of GSLB (global server load balance) technology
Audio resource settings for U3D resource management
Use classname to modify style properties
Remember a lazy query error