当前位置:网站首页>540. Single element in ordered array
540. Single element in ordered array
2022-07-03 22:21:00 【Phoenix_ ZengHao】
subject
540. A single element in an ordered array
The main idea of the topic
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 .
Examples

Data scale

Ideas 1
Consider the simplest way first : Because other elements appear twice , Only one element will appear once , Then the XOR value of the number that appears twice is 0, Then the XOR value of all numbers is the value of the element that appears only once .
Time complexity O ( n ) O(n) O(n)
Code 1
class Solution {
public:
int singleNonDuplicate(vector<int>& nums) {
int ans=0;
for(int i=0;i<nums.size();i++){
ans^=nums[i];
}
return ans;
}
};
Ideas 2
Consider how to O ( l o g n ) O(logn) O(logn) Realization : The subscript corresponding to the first of the paired elements must be even , The subscript corresponding to the second of the paired elements must be odd . Then consider the existence of a single element , If the subscript of a single element is x x x, So subscript x x x Before ( On the left ) The position of still satisfies the above conclusion , And subscripts x x x after ( On the right ) Due to x x x Insertion , Cause the conclusion to flip . So you can use dichotomy to judge contraction .
Time complexity O ( l o g n ) O(logn) O(logn)
Code 2
class Solution {
public:
int singleNonDuplicate(vector<int>& nums) {
int l=0,r=nums.size()-1,ans=0;
while(l<r){
int mid=(l+r)/2;
if(nums[mid]==nums[mid^1]){
l=mid+1;
}
else{
r=mid;
ans=mid;
}
}
return nums[r];
}
};
边栏推荐
- Miscellaneous things that don't miss the right business
- Label coco format data and format data in the upper left corner and lower right corner are mutually converted
- Exclusive interview with the person in charge of openkruise: to what extent has cloud native application automation developed now?
- Collection | pytoch common loss function disassembly
- Go Technology Daily (2022-02-13) - Summary of experience in database storage selection
- Codeforces Round #768 (Div. 1)(A-C)
- Cognitive fallacy: what is Fredkin's paradox
- [secretly kill little partner pytorch20 days] - [day3] - [example of text data modeling process]
- Report on the current situation and development trend of ethoxylated sodium alkyl sulfate industry in the world and China Ⓞ 2022 ~ 2027
- UC Berkeley proposes a multitask framework slip
猜你喜欢

How to solve the problem of computer networking but showing no Internet connection

Awk getting started to proficient series - awk quick start

Some 5000+ likes, the development notes of a director of cosmic factory, leaked

Data consistency between redis and database
![[dynamic planning] counting garlic customers: the log of garlic King (the longest increasing public subsequence)](/img/29/543dce2f24130d22c1824385fbfa8f.jpg)
[dynamic planning] counting garlic customers: the log of garlic King (the longest increasing public subsequence)

Team collaborative combat penetration tool CS artifact cobalt strike

What is the difference between res.send() and res.end() in the node express framework

Yyds dry goods inventory Spring Festival "make" your own fireworks

Blue Bridge Cup Guoxin Changtian single chip microcomputer -- software environment (II)

Go Technology Daily (2022-02-13) - Summary of experience in database storage selection
随机推荐
QGIS grid processing DEM data reclassification
Development mode and Prospect of China's IT training industry strategic planning trend report Ⓣ 2022 ~ 2028
油猴插件
Plug - in Oil Monkey
Blue Bridge Cup Guoxin Changtian MCU -- program download (III)
[actual combat record] record the whole process of the server being attacked (redis vulnerability)
[Android reverse] application data directory (files data directory | lib application built-in so dynamic library directory | databases SQLite3 database directory | cache directory)
string
Common problems in multi-threaded learning (I) ArrayList under high concurrency and weird hasmap under concurrency
Uboot migration
Data consistency between redis and database
UC Berkeley proposes a multitask framework slip
Dynamic research and future planning analysis report of China's urban water supply industry Ⓝ 2022 ~ 2028
Ansible common usage scenarios
[sg function] lightoj Partitioning Game
China's TPMS industry demand forecast and future development trend analysis report Ⓐ 2022 ~ 2028
Exclusive interview with the person in charge of openkruise: to what extent has cloud native application automation developed now?
Intimacy communication -- [repair relationship] - use communication to heal injuries
What indicators should be paid attention to in current limit monitoring?
STM32 multi serial port implementation of printf -- Based on cubemx