当前位置:网站首页>LeetCode 540. A single element in an ordered array
LeetCode 540. A single element in an ordered array
2022-07-03 22:07:00 【Daylight629】
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
Two 、 Method 1
Pure XOR
class Solution {
public int singleNonDuplicate(int[] nums) {
int res = 0;
for (int num : nums) {
res ^= num;
}
return res;
}
}
Complexity analysis
Time complexity :O(n).
Spatial complexity :O(1).
Two 、 Method 1
Two points + Exclusive or
class Solution {
public int singleNonDuplicate(int[] nums) {
int l = 0;
int r = nums.length - 1;
while (l < r) {
int mid = l + ((r - l) >> 1);
if (nums[mid] == nums[mid ^ 1]) {
l = mid + 1;
} else {
r = mid;
}
}
return nums[l];
}
}
Complexity analysis
Time complexity :O(logn), among n It's an array nums The length of . You need to binary search in the range of the whole array , The time complexity of binary search is O(logn).
Spatial complexity :O(1).
边栏推荐
- Netfilter ARP log
- Covariance
- Analysis report on the development prospect and investment strategy of global and Chinese modular automation systems Ⓟ 2022 ~ 2027
- treevalue——Master Nested Data Like Tensor
- Après 90 ans, j'ai démissionné pour démarrer une entreprise et j'ai dit que j'allais détruire la base de données Cloud.
- On my first day at work, this API timeout optimization put me down!
- WiFi 2.4g/5g/6g channel distribution
- Under the double reduction policy, research travel may become a big winner
- Functions and differences between static and Const
- Getting started with DOM
猜你喜欢
Nacos common configuration
Morning flowers and evening flowers
Blue Bridge Cup Guoxin Changtian MCU -- program download (III)
A little understanding of GSLB (global server load balance) technology
2022 G3 boiler water treatment registration examination and G3 boiler water treatment examination papers
Imitation Netease cloud music applet
How PHP gets all method names of objects
Bluebridge cup Guoxin Changtian single chip microcomputer -- detailed explanation of schematic diagram (IV)
Common SQL sets
常用sql集合
随机推荐
Bluebridge cup Guoxin Changtian single chip microcomputer -- detailed explanation of schematic diagram (IV)
UC Berkeley proposes a multitask framework slip
Intimacy communication -- [repair relationship] - use communication to heal injuries
Supply and demand situation and market scale calculation report of China's portable energy storage power PES industry Ⓛ 2022 ~ 2028
A little understanding of GSLB (global server load balance) technology
Exness: the Central Bank of England will raise interest rates again in March, and inflation is coming
Global and Chinese market of gallic acid 2022-2028: Research Report on technology, participants, trends, market size and share
Common SQL sets
Development mode and Prospect of China's IT training industry strategic planning trend report Ⓣ 2022 ~ 2028
Cesium terrain clipping draw polygon clipping
WFC900M-Network_ Card/Qualcomm-Atheros-AR9582-2T-2R-MIMO-802.11-N-900M-high-power-Mini-PCIe-Wi-Fi-Mod
油猴插件
Global and Chinese market of wireless hard disk 2022-2028: Research Report on technology, participants, trends, market size and share
flink sql-client 退出,表就会被清空怎么办?
The latest analysis of crane driver (limited to bridge crane) in 2022 and the test questions and analysis of crane driver (limited to bridge crane)
常用sql集合
JS notes (III)
No more! Technical team members resign collectively
An expression that regularly matches one of two strings
鹏城杯 WEB_WP