当前位置:网站首页>540. Single element in ordered array
540. Single element in ordered array
2022-07-01 18:17:00 【Listen 1024】
>540. A single element in an ordered array <
>singleNonDuplicate<

One 、 Their thinking
1、 Solution 1 ( Java )
Solution ideas : XOR pairing + Two points search
adopt An operation Medium mid ^ 1 Exclusive or Operator to unify parity
- i For even when :
i ^ 1 == i + 1, The result is the odd number after the original even number plus one - i In an odd number of :
i ^ 1 == i - 1, The result is the even number after subtracting one from the original odd number
/** * @author listen1024 * @date 2022-2-14 * 540. A single element in an ordered array */
class Solution {
public int singleNonDuplicate(int[] nums) {
int low = 0;
int high = nums.length - 1;
while (low < high) {
int mid = (low + high) /2;
if (nums[mid] == nums[mid ^ 1]){
low = mid + 1;
} else {
high = mid;
}
}
return nums[low];
}
}
The screenshot of the running result is as follows :
边栏推荐
- Depth first traversal and breadth first traversal [easy to understand]
- Happy new year | 202112 monthly summary
- PHP implements sensitive word filtering system "suggestions collection"
- ACM mm 2022 video understanding challenge video classification track champion autox team technology sharing
- February 16, 2022 Daily: graph neural network self training method under distribution and migration
- Distributed task queue: Celery usage record
- Euler function: find the number of numbers less than or equal to N and coprime with n
- Function, condition, regular expression
- 股票万1免5证券开户是合理安全的吗,怎么讲
- [beauty detection artifact] come on, please show your unique skill (is this beauty worthy of the audience?)
猜你喜欢

Fresh, 2022 advanced Android interview must know 100 questions (interview questions + answer analysis)

The new server is packaged with the source code of H5 mall with an operation level value of several thousand

Penetration practice vulnhub range Tornado

Kia recalls some K3 new energy with potential safety hazards

Countdownlatch blocking wait for multithreading concurrency

Common design parameters of solid rocket motor

Mujoco model learning record

Explain in detail the process of realizing Chinese text classification by CNN

Review Net 20th anniversary development and 51aspx growth

From comedians to NBA Zhan Huang, check the encrypted advertisements during this super bowl
随机推荐
Wechat applet blind box - docking wechat payment
Work and leisure suggestions of old programmers
开发那些事儿:EasyCVR平台添加播放地址鉴权
Is it reasonable and safe to open a securities account for 10000 shares free of charge? How to say
Gameframework eating guide
Kia recalls some K3 new energy with potential safety hazards
Fix the black screen caused by iPhone system failure
New 95 community system whole station source code
The difference and relationship between iteratible objects, iterators and generators
js如何将带有分割符的字符串转化成一个n维数组
What are the six steps of the software development process? How to draw software development flow chart?
Subnet division and summary
股票万1免5证券开户是合理安全的吗,怎么讲
Relationship between sensor size, pixel, dpi resolution, inch and millimeter
Detailed explanation of select in golang
Quick foundation of group theory (5): generators, Kelley graphs, orbits, cyclic graphs, and "dimensions" of groups?
Mujoco's biped robot Darwin model
Redis master-slave realizes 10 second check and recovery
Yolov5 practice: teach object detection by hand
Unity3d extended toolbar