当前位置:网站首页>【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];
}
}
边栏推荐
- Launpad | 基础知识
- Global and Chinese markets for laser assisted liposuction (LAL) devices 2022-2028: Research Report on technology, participants, trends, market size and share
- Research Report on the current market situation and development prospects of calcium sulfate whiskers in China (2022 Edition)
- ArcGIS application (XXII) ArcMap loading lidar Las format data
- awk从入门到入土(9)循环语句
- Flutter 小技巧之 ListView 和 PageView 的各种花式嵌套
- 2022-2028 global small batch batch batch furnace industry research and trend analysis report
- Flutter tips: various fancy nesting of listview and pageview
- Service call feign of "micro service"
- After unplugging the network cable, does the original TCP connection still exist?
猜你喜欢

After unplugging the network cable, does the original TCP connection still exist?

2022-2028 global optical transparency industry research and trend analysis report
](/img/3f/4d8f4c77d9fde5dd3f53ef890ecfa8.png)
C語言-入門-基礎-語法-[運算符,類型轉換](六)

Codeforces Global Round 21(A-E)

If you can quickly generate a dictionary from two lists

Target detection -- intensive reading of yolov3 paper

Solve the problem of "Chinese garbled MySQL fields"

HMS core helps baby bus show high-quality children's digital content to global developers

Sequence model
](/img/dc/5c8077c10cdc7ad6e6f92dedfbe797.png)
C语言-入门-基础-语法-[变量,常亮,作用域](五)
随机推荐
Launpad | basic knowledge
Trim leading or trailing characters from strings- Trim leading or trailing characters from a string?
《网络是怎么样连接的》读书笔记 - FTTH
DR6018-CP01-wifi6-Qualcomm-IPQ6010-IPQ6018-FAMILY-2T2R-2.5G-ETH-port-CP01-802-11AX-MU-MIMO-OFDMA
2022-2028 global probiotics industry research and trend analysis report
"How to connect the network" reading notes - Web server request and response (4)
awk从入土到入门(10)awk内置函数
awk从入门到入土(5)简单条件匹配
老掉牙的 synchronized 锁优化,一次给你讲清楚!
Review of last week's hot spots (6.27-7.3)
China battery grade manganese sulfate Market Forecast and strategic consulting report (2022 Edition)
Sword finger offer 30 contains the stack of Min function
Global and Chinese market of planar waveguide optical splitter 2022-2028: Research Report on technology, participants, trends, market size and share
Relationship and operation of random events
Awk from digging into the ground to getting started (10) awk built-in functions
Research Report on research and investment prospects of China's testing machine industry (2022 Edition)
2022-2028 global elastic strain sensor industry research and trend analysis report
awk从入门到入土(18)gawk线上手册
Opencv environment construction (I)
Codeforces Round #793 (Div. 2)(A-D)