当前位置:网站首页>【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];
}
}
边栏推荐
- In depth research and investment strategy report on China's hydraulic parts industry (2022 Edition)
- 什么是uid?什么是Auth?什么是验证器?
- Target detection -- intensive reading of yolov3 paper
- Investment analysis and future production and marketing demand forecast report of China's paper industry Ⓥ 2022 ~ 2028
- After unplugging the network cable, does the original TCP connection still exist?
- Sequence model
- C language - Introduction - Foundation - syntax - [operators, type conversion] (6)
- C语言-入门-基础-语法-数据类型(四)
- Live in a dream, only do things you don't say
- awk从入门到入土(5)简单条件匹配
猜你喜欢
2022-2028 global edible probiotic raw material industry research and trend analysis report
How should PMP learning ideas be realized?
Target detection -- intensive reading of yolov3 paper
Solve the problem of "Chinese garbled MySQL fields"
[C Advanced] file operation (2)
Opencv environment construction (I)
Dede plug-in (multi-function integration)
C语言-入门-基础-语法-[变量,常亮,作用域](五)
2022-2028 global elastic strain sensor industry research and trend analysis report
Mac platform forgets the root password of MySQL
随机推荐
Global and Chinese market of sampler 2022-2028: Research Report on technology, participants, trends, market size and share
How to write unit test cases
HMS core helps baby bus show high-quality children's digital content to global developers
Codeforces Round #750 (Div. 2)(A,B,C,D,F1)
《网络是怎么样连接的》读书笔记 - 认识网络基础概念(一)
C语言-入门-基础-语法-[运算符,类型转换](六)
Dynamic analysis and development prospect prediction report of high purity manganese dioxide in the world and China Ⓡ 2022 ~ 2027
Relationship and operation of random events
Analysis report on the production and marketing demand and investment forecast of tellurium dioxide in the world and China Ⓣ 2022 ~ 2027
26. Delete duplicates in the ordered array (fast and slow pointer de duplication)
Global and Chinese market of bipolar generators 2022-2028: Research Report on technology, participants, trends, market size and share
C语言-入门-基础-语法-[主函数,头文件](二)
UML 时序图[通俗易懂]
After unplugging the network cable, does the original TCP connection still exist?
How to ensure the uniqueness of ID in distributed environment
《网络是怎么样连接的》读书笔记 - FTTH
Les différents modèles imbriqués de listview et Pageview avec les conseils de flutter
Launpad | 基础知识
【LeetCode 42】501. Mode in binary search tree
You can see the employment prospects of PMP project management