当前位置:网站首页>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];
}
};
边栏推荐
- The difference between SRAM and DRAM
- Collection | pytoch common loss function disassembly
- China HDI market production and marketing demand and investment forecast analysis report Ⓢ 2022 ~ 2028
- Harbor integrated LDAP authentication
- On my first day at work, this API timeout optimization put me down!
- [sg function]split game (2020 Jiangxi university student programming competition)
- 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
- [actual combat record] record the whole process of the server being attacked (redis vulnerability)
- How PHP gets all method names of objects
- regular expression
猜你喜欢
string
Dahua series books
Functions and differences between static and Const
QGIS grid processing DEM data reclassification
4 environment construction -standalone ha
pivot ROP Emporium
Leetcode week 4: maximum sum of arrays (shape pressing DP bit operation)
Compréhension de la technologie gslb (Global Server load balance)
Data consistency between redis and database
Yyds dry goods inventory Spring Festival "make" your own fireworks
随机推荐
Sed、Awk
6.2 normalization 6.2.5 third normal form (3NF)
DR-AP40X9-A-Qualcomm-IPQ-4019-IPQ-4029-5G-4G-LTE-aluminum-body-dual-band-wifi-router-2.4GHZ-5GHz-QSD
IPhone development swift foundation 09 assets
China's TPMS industry demand forecast and future development trend analysis report Ⓐ 2022 ~ 2028
DR882-Qualcomm-Atheros-QCA9882-2T2R-MIMO-802.11ac-Mini-PCIe-Wi-Fi-Module-5G-high-power
Investment analysis and prospect trend prediction report of China's boron nitride industry Ⓨ 2022 ~ 2028
Data consistency between redis and database
Go Technology Daily (2022-02-13) - Summary of experience in database storage selection
Teach you how to run two or more MySQL databases at the same time in one system
[flax high frequency question] leetcode 426 Convert binary search tree to sorted double linked list
Leetcode week 4: maximum sum of arrays (shape pressing DP bit operation)
Compréhension de la technologie gslb (Global Server load balance)
English topic assignment (28)
Report on the development status and investment planning trends of China's data center industry Ⓡ 2022 ~ 2028
[dynamic programming] Jisuan Ke: Jumping stake (variant of the longest increasing subsequence)
Cesium terrain clipping draw polygon clipping
Code in keil5 -- use the code formatting tool astyle (plug-in)
国泰君安证券开户是安全可靠的么?怎么开国泰君安证券账户
[automation operation and maintenance novice village] flask-2 certification