当前位置:网站首页>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];
}
};
边栏推荐
- Awk getting started to proficient series - awk quick start
- 3 environment construction -standalone
- Yyds dry goods inventory Spring Festival "make" your own fireworks
- Teach you how to run two or more MySQL databases at the same time in one system
- Correlation
- Code in keil5 -- use the code formatting tool astyle (plug-in)
- [automation operation and maintenance novice village] flask-2 certification
- Pooling idea: string constant pool, thread pool, database connection pool
- Cesium terrain clipping draw polygon clipping
- Mysql database - Advanced SQL statement (I)
猜你喜欢
Yyds dry goods inventory Spring Festival "make" your own fireworks
string
2022 electrician (elementary) examination questions and electrician (elementary) registration examination
Farmersworld farmers world, no faith, how to talk about success?
The overseas listing of Shangmei group received feedback, and brands such as Han Shu and Yiye have been notified for many times and received attention
How to solve win10 black screen with only mouse arrow
Redis single thread and multi thread
2022 G3 boiler water treatment registration examination and G3 boiler water treatment examination papers
Harbor integrated LDAP authentication
Data consistency between redis and database
随机推荐
Exclusive interview with the person in charge of openkruise: to what extent has cloud native application automation developed now?
Luogu deep foundation part 1 Introduction to language Chapter 6 string and file operation
regular expression
Leetcode problem solving - 235 Nearest common ancestor of binary search tree
[secretly kill little partner pytorch20 days] - [day3] - [example of text data modeling process]
(POJ - 2912) rochambau (weighted concurrent search + enumeration)
[dynamic planning] counting garlic customers: the log of garlic King (the longest increasing public subsequence)
js demo 计算本年度还剩下多少天
Cognitive fallacy: what is Fredkin's paradox
Label coco format data and format data in the upper left corner and lower right corner are mutually converted
A little understanding of GSLB (global server load balance) technology
Programming language (2)
[SRS] build a specified version of SRS
Exness: the Central Bank of England will raise interest rates again in March, and inflation is coming
What is the difference between res.send() and res.end() in the node express framework
Codeforces Round #768 (Div. 1)(A-C)
[sg function] lightoj Partitioning Game
Oil monkey plug-in
Team collaborative combat penetration tool CS artifact cobalt strike
How to solve the problem of requiring a password when accessing your network neighborhood on your computer