当前位置:网站首页>[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
边栏推荐
- C import Xls data method summary I (upload files and create Workbooks)
- Hash table, string hash (special KMP)
- Small program graduation project based on wechat reservation small program graduation project opening report reference
- Notice on Soliciting Opinions on the draft of information security technology mobile Internet application (APP) life cycle security management guide
- All ceramic crowns - current market situation and future development trend
- Special copy UML notes
- Magical usage of edge browser (highly recommended by program ape and student party)
- GUI application: socket network chat room
- IPv6 experiment
- Hbuilder link Xiaoyao simulator
猜你喜欢
Technical practice online fault analysis and solutions (Part 1)
C import Xls data method summary II (save the uploaded file to the DataTable instance object)
Openbionics exoskeleton project introduction | bciduino community finishing
技術實踐|線上故障分析及解决方法(上)
Jerry's modification setting status [chapter]
中电资讯-信贷业务数字化转型如何从星空到指尖?
IPv6 experiment
Applet graduation design is based on wechat course appointment registration. Applet graduation design opening report function reference
CLP information - how does the digital transformation of credit business change from star to finger?
LeetCode226. Flip binary tree
随机推荐
Introduction to Tianchi news recommendation: 4 Characteristic Engineering
Is Shengang securities company as safe as other securities companies
All ceramic crowns - current market situation and future development trend
机器学习基础:用 Lasso 做特征选择
The latest analysis of hoisting machinery command in 2022 and free examination questions of hoisting machinery command
Query efficiency increased by 10 times! Three optimization schemes to help you solve the deep paging problem of MySQL
Containerization technology stack
2022 electrician (elementary) examination question bank and electrician (elementary) simulation examination question bank
How to use AHAS to ensure the stability of Web services?
Ka! Why does the seat belt suddenly fail to pull? After reading these pictures, I can't stop wearing them
Difference between value and placeholder
LeetCode226. Flip binary tree
Magical usage of edge browser (highly recommended by program ape and student party)
Logical operator, displacement operator
Day05 branch and loop (II)
Why can't it run (unresolved)
CLP information - how does the digital transformation of credit business change from star to finger?
Pyrethroid pesticide intermediates - market status and future development trend
Long article review: entropy, free energy, symmetry and dynamics in the brain
长文综述:大脑中的熵、自由能、对称性和动力学