当前位置:网站首页>【leetcode】540. A single element in an ordered array
【leetcode】540. A single element in an ordered array
2022-07-04 09:16:00 【Chinese fir sauce_】
subject :
540. 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
Dichotomy thinking :
- mid and mid^1 Are the subscripts of two adjacent numbers (∵ When mid For even when , The number adjacent to it is mid+1=mid ^ 1; When mid In an odd number of , The number adjacent to it is mid-1=mid ^ 1).
- We just need to compare whether the two adjacent numbers are equal , If equal , What we are looking for x The subscript of is greater than mid, Then the left boundary moves to the right low=mid+1; If it's not equal , be x The subscript of is less than or equal to mid, Then the right boundary moves to the left high=mid.
- Adjust the boundary and continue the binary search , Until the subscript is determined x Value .
class Solution {
public int singleNonDuplicate(int[] nums) {
/** An operation O(n) */
// int num = 0;
// for(int i : nums){
// num ^= i;
// }
// return num;
/** Dichotomy :O(log n) */
int low = 0, high = nums.length - 1;
while (low < high) {
int mid = (high - low) / 2 + low;
if (nums[mid] == nums[mid ^ 1]) {
low = mid + 1;
} else {
high = mid;
}
}
return nums[low];
}
}
边栏推荐
- What is uid? What is auth? What is a verifier?
- Codeforces Round #750 (Div. 2)(A,B,C,D,F1)
- C language - Introduction - Foundation - syntax - [variable, constant light, scope] (V)
- How should PMP learning ideas be realized?
- 2022-2028 global visual quality analyzer industry research and trend analysis report
- [error record] no matching function for call to 'cacheflush' cacheflush();)
- Lauchpad X | 模式
- Awk from getting started to digging in (9) circular statement
- 2022-2028 global special starch industry research and trend analysis report
- How to batch change file extensions in win10
猜你喜欢
Opencv environment construction (I)
After unplugging the network cable, does the original TCP connection still exist?
Codeforces Round #803 (Div. 2)(A-D)
C语言-入门-基础-语法-数据类型(四)
2022-2028 global probiotics industry research and trend analysis report
You can see the employment prospects of PMP project management
HMS core helps baby bus show high-quality children's digital content to global developers
Sword finger offer 30 contains the stack of Min function
Educational Codeforces Round 119 (Rated for Div. 2)
Turn: excellent managers focus not on mistakes, but on advantages
随机推荐
Codeforces Global Round 21(A-E)
At the age of 30, I changed to Hongmeng with a high salary because I did these three things
Global and Chinese market of planar waveguide optical splitter 2022-2028: Research Report on technology, participants, trends, market size and share
Launpad | basic knowledge
Educational Codeforces Round 119 (Rated for Div. 2)
awk从入门到入土(11)awk getline函数详解
Mac platform forgets the root password of MySQL
CLion-控制台输出中文乱码
Reading notes on how to connect the network - tcp/ip connection (II)
Markdown syntax
Les différents modèles imbriqués de listview et Pageview avec les conseils de flutter
ArcGIS application (XXII) ArcMap loading lidar Las format data
Tkinter Huarong Road 4x4 tutorial II
Awk from entry to earth (8) array
Use Alibaba cloud NPM image acceleration
Simulate EF dbcontext with MOQ - mocking EF dbcontext with MOQ
Trees and graphs (traversal)
Global and Chinese market of sampler 2022-2028: Research Report on technology, participants, trends, market size and share
If you can quickly generate a dictionary from two lists
Awk from entry to earth (14) awk output redirection