当前位置:网站首页>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).
边栏推荐
- How to install sentinel console
- 4. Data splitting of Flink real-time project
- flink sql-client 退出,表就会被清空怎么办?
- Decompile and modify the non source exe or DLL with dnspy
- How PHP gets all method names of objects
- What is the difference between res.send() and res.end() in the node express framework
- Global and Chinese market of wireless hard disk 2022-2028: Research Report on technology, participants, trends, market size and share
- The 14th five year plan for the construction of Chinese Enterprise Universities and the feasibility study report on investment Ⓓ 2022 ~ 2028
- Investment planning analysis and prospect prediction report of China's satellite application industry during the 14th five year plan Ⓑ 2022 ~ 2028
- gslb(global server load balance)技術的一點理解
猜你喜欢
[dynamic planning] counting garlic customers: the log of garlic King (the longest increasing public subsequence)
Preliminary analysis of smart microwave radar module
Compréhension de la technologie gslb (Global Server load balance)
IPhone development swift foundation 09 assets
Yiwen teaches you how to choose your own NFT trading market
Team collaborative combat penetration tool CS artifact cobalt strike
Farmersworld farmers world, no faith, how to talk about success?
On my first day at work, this API timeout optimization put me down!
使用dnSpy对无源码EXE或DLL进行反编译并且修改
2022 G3 boiler water treatment registration examination and G3 boiler water treatment examination papers
随机推荐
Preliminary analysis of smart microwave radar module
Uboot migration
What should the future of the Internet be like when Silicon Valley employees flee the big factory and rush to Web3| Footprint Analytics
Investment planning analysis and prospect prediction report of China's satellite application industry during the 14th five year plan Ⓑ 2022 ~ 2028
Leetcode problem solving - 230 The k-th smallest element in the binary search tree
WFC900M-Network_ Card/Qualcomm-Atheros-AR9582-2T-2R-MIMO-802.11-N-900M-high-power-Mini-PCIe-Wi-Fi-Mod
Blue Bridge Cup Guoxin Changtian single chip microcomputer -- software environment (II)
js demo 计算本年度还剩下多少天
Why use pycharm to run the use case successfully but cannot exit?
Electronic tube: Literature Research on basic characteristics of 6j1
Mysql database - Advanced SQL statement (I)
treevalue——Master Nested Data Like Tensor
Luogu deep foundation part 1 Introduction to language Chapter 7 functions and structures
Control loop of program (while loop)
Global and Chinese market of telematics boxes 2022-2028: Research Report on technology, participants, trends, market size and share
What indicators should be paid attention to in current limit monitoring?
treevalue——Master Nested Data Like Tensor
4. Data splitting of Flink real-time project
2022 electrician (elementary) examination questions and electrician (elementary) registration examination
How to obtain opensea data through opensea JS