当前位置:网站首页>[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 
边栏推荐
- Magical usage of edge browser (highly recommended by program ape and student party)
- Fundamentals of machine learning: feature selection with lasso
- Experimental animal models - current market situation and future development trend
- Containerization technology stack
- Huawei cloud micro certification Huawei cloud computing service practice has been stable
- Software product download collection
- Maximum likelihood method, likelihood function and log likelihood function
- Cancer biopsy instruments and kits - market status and future development trends
- MySQL - use of aggregate functions and group by groups
- Flex flexible layout, box in the middle of the page
猜你喜欢

Applet graduation design is based on wechat course appointment registration. Applet graduation design opening report function reference

“疫”起坚守 保障数据中台服务“不打烊”

Pratique technique | analyse et solution des défaillances en ligne (Partie 1)

Avoid playing with super high conversion rate in material minefields

SQL statement

Maximum likelihood method, likelihood function and log likelihood function

Three layer switching ②

Small program graduation project based on wechat reservation small program graduation project opening report reference

A malware detection method for checking PLC system using satisfiability modulus theoretical model

In the process of seeking human intelligent AI, meta bet on self supervised learning
随机推荐
Sequence sorting of basic exercises of test questions
It's corrected. There's one missing < /script >, why doesn't the following template come out?
Mongodb learning notes: command line tools
Luogu p1309 Swiss wheel
Meta metauniverse female safety problems occur frequently, how to solve the relevant problems in the metauniverse?
Three layer switching ②
2022 new examination questions for safety management personnel of hazardous chemical business units and certificate examination for safety management personnel of hazardous chemical business units
Cancer biopsy instruments and kits - market status and future development trends
Is Shengang securities company as safe as other securities companies
Mobile phone battery - current market situation and future development trend
MPLS③
Flex flexible layout, box in the middle of the page
Applet graduation project based on wechat selection voting applet graduation project opening report function reference
Msp32c3 board connection MSSQL method
[typora installation package] old typera installation package, free version
Three layer switching ①
Meta metauniverse female safety problems occur frequently. How to solve the related problems in the metauniverse?
mysql使用视图报错,EXPLAIN/SHOW can not be issued; lacking privileges for underlying table
Pratique technique | analyse et solution des défaillances en ligne (Partie 1)
IPv6 experiment