当前位置:网站首页>[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 
边栏推荐
- Notice on Soliciting Opinions on the draft of information security technology mobile Internet application (APP) life cycle security management guide
- Which insurance products can the elderly buy?
- Introduction to superresolution
- 中电资讯-信贷业务数字化转型如何从星空到指尖?
- MySQL statement learning record
- Development of user-defined navigation bar in uniapp
- Prose article appreciation - the rain in the warm country has never changed into cold, hard and brilliant flowers. Knowledgeable people think he is monotonous, and he thinks he is unlucky, doesn't he?
- mysql使用视图报错,EXPLAIN/SHOW can not be issued; lacking privileges for underlying table
- Jerry's watch listens to the message notification of the target third-party software and pushes the message to the device [article]
- Typescript basic knowledge sorting
猜你喜欢

Make drop-down menu

Pratique technique | analyse et solution des défaillances en ligne (Partie 1)
![Jerry's synchronous weather information to equipment [chapter]](/img/a9/e09bbf62161ea8ba60b0fce3fb5f89.jpg)
Jerry's synchronous weather information to equipment [chapter]

CLP information - how does the digital transformation of credit business change from star to finger?

Ka! Why does the seat belt suddenly fail to pull? After reading these pictures, I can't stop wearing them

Luogu p1309 Swiss wheel

MySQL introduction - functions (various function statistics, exercises, details, tables)

In the process of seeking human intelligent AI, meta bet on self supervised learning

SRCNN:Learning a Deep Convolutional Network for Image Super-Resolution

LeetCode226. Flip binary tree
随机推荐
Force deduction solution summary 1189- maximum number of "balloons"
Skku| autonomous handover decision of UAV Based on deep reinforcement learning
Fundamentals of machine learning: feature selection with lasso
The force deduction method summarizes the single elements in the 540 ordered array
Pratique technique | analyse et solution des défaillances en ligne (Partie 1)
HackTheBox-baby breaking grad
Remember a lazy query error
Chinese Mitten Crab - current market situation and future development trend
TP5 automatic registration hook mechanism hook extension, with a complete case
Future source code view -juc series
Msp32c3 board connection MSSQL method
Meta metauniverse female safety problems occur frequently. How to solve the related problems in the metauniverse?
Jerry's watch information type table [chapter]
How can enterprises optimize the best cost of cloud computing?
File contains vulnerability summary
Ka! Why does the seat belt suddenly fail to pull? After reading these pictures, I can't stop wearing them
Three layer switching ①
Huawei cloud micro certification Huawei cloud computing service practice has been stable
Should enterprises start building progressive web applications?
Applet graduation project based on wechat selection voting applet graduation project opening report function reference