当前位置:网站首页>[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 
边栏推荐
- Hbuilder link Xiaoyao simulator
- Conditional statements of shell programming
- Which insurance products can the elderly buy?
- ThinkPHP uses redis to update database tables
- Query efficiency increased by 10 times! Three optimization schemes to help you solve the deep paging problem of MySQL
- Cancer biopsy instruments and kits - market status and future development trends
- PMP 考试常见工具与技术点总结
- A little understanding of GSLB (global server load balance) technology
- Install the pit that the electron has stepped on
- Jerry's update contact [article]
猜你喜欢

Lightweight Pyramid Networks for Image Deraining

IPv6 experiment

String hash, find the string hash value after deleting any character, double hash

技術實踐|線上故障分析及解决方法(上)
![Jerry's synchronous weather information to equipment [chapter]](/img/a9/e09bbf62161ea8ba60b0fce3fb5f89.jpg)
Jerry's synchronous weather information to equipment [chapter]

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

Audio resource settings for U3D resource management

Force buckle day32

Huawei rip and BFD linkage

2020-12-02 SSM advanced integration Shang Silicon Valley
随机推荐
Infiltration learning diary day19
Summary of common tools and technical points of PMP examination
Huawei BFD and NQA
Pesticide synergist - current market situation and future development trend
Applet graduation project based on wechat selection voting applet graduation project opening report function reference
File contains vulnerability summary
Idsia & supsi & usi | continuous control behavior learning and adaptive robot operation based on Reinforcement Learning
What are the advantages and disadvantages of data center agents?
MySQL introduction - functions (various function statistics, exercises, details, tables)
Fundamentals of machine learning: feature selection with lasso
Query efficiency increased by 10 times! Three optimization schemes to help you solve the deep paging problem of MySQL
2022 electrician (elementary) examination question bank and electrician (elementary) simulation examination question bank
MySQL -- Introduction and use of single line functions
When the watch system of Jerry's is abnormal, it is used to restore the system [chapter]
Flex flexible layout, box in the middle of the page
QML add gradient animation during state transition
Which insurance products can the elderly buy?
Cancer biopsy instruments and kits - market status and future development trends
How programmers find girlfriends through blind dates
Maximum entropy model