当前位置:网站首页>leetcode-540. A single element in an ordered array
leetcode-540. A single element in an ordered array
2022-07-03 20:49:00 【Eager to be the winner of loneliness】
- A single element in an ordered array
Give you an ordered array of integers only , Each of these elements will appear twice , Only one number will appear once .
Please find and return the number that only appears once .
The solution you design must meet O(log n) Time complexity and O(1) Spatial complexity .
Example 1:
Input : nums = [1,1,2,3,3,4,4,8,8]
Output : 2
Example 2:
Input : nums = [3,3,7,7,10,11,11]
Output : 10
Tips :
1 <= nums.length <= 105
0 <= nums[i] <= 105
class Solution {
public int singleNonDuplicate(int[] nums) {
int min=0,max=nums.length-1;
while(min<max){
int mid=(min+max)/2; // Or written (max-min)/2+min
if(nums[mid]==nums[mid^1]){
// Be careful : Here is bitwise XOR
min=mid+1;
}
else{
max=mid;
}
}
return nums[min];
}
}
边栏推荐
- Haven't expressed the artifact yet? Valentine's Day is coming. Please send her a special gift~
- 18、 MySQL -- index
- thrift go
- Hcie security Day10: six experiments to understand VRRP and reliability
- 【leetcode】1027. Longest arithmetic sequence (dynamic programming)
- Etcd raft Based Consistency assurance
- Do you really know how old you are?
- Line segment tree blue book explanation + classic example acwing 1275 Maximum number
- Oak-d raspberry pie cloud project [with detailed code]
- 19、 MySQL -- SQL statements and queries
猜你喜欢
18、 MySQL -- index
[gd32l233c-start] 5. FLASH read / write - use internal flash to store data
2.2 integer
thrift go
Introduction to golang garbage collection
Do you really know how old you are?
【愚公系列】2022年7月 Go教学课程 002-Go语言环境安装
C 10 new feature [caller parameter expression] solves my confusion seven years ago
The global industrial design revenue in 2021 was about $44360 million, and it is expected to reach $62720 million in 2028. From 2022 to 2028, the CAGR was 5.5%
2022 high voltage electrician examination and high voltage electrician reexamination examination
随机推荐
Cesiumjs 2022 ^ source code interpretation [7] - Analysis of the request and loading process of 3dfiles
LabVIEW training
Recommendation of books related to strong foundation program mathematics
Brief analysis of ref nerf
Do you really know how old you are?
MDM mass data synchronization test verification
2022 safety officer-c certificate examination and safety officer-c certificate registration examination
内存分析器 (MAT)
Transformer structure analysis and the principle of blocks in it
同花顺开户注册安全靠谱吗?有没有风险的?
2022 melting welding and thermal cutting examination materials and free melting welding and thermal cutting examination questions
Oak-d raspberry pie cloud project [with detailed code]
First knowledge of database
不同业务场景该如何选择缓存的读写策略?
jvm jni 及 pvm pybind11 大批量数据传输及优化
Qt6 QML Book/Qt Quick 3D/基础知识
Basic number theory -- Chinese remainder theorem
TLS environment construction and plaintext analysis
How can the outside world get values when using nodejs to link MySQL
Print linked list from end to end