当前位置:网站首页>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];
}
};
边栏推荐
- Cognitive fallacy: Wittgenstein's ruler
- 股票炒股开户注册安全靠谱吗?有没有风险的?
- Harbor integrated LDAP authentication
- 2022 G3 boiler water treatment registration examination and G3 boiler water treatment examination papers
- Blue Bridge Cup Guoxin Changtian single chip microcomputer -- software environment (II)
- Awk getting started to proficient series - awk quick start
- Yyds dry goods inventory Prometheus alarm Art
- Uboot migration
- Programming language (1)
- Report on the development status and investment planning trends of China's data center industry Ⓡ 2022 ~ 2028
猜你喜欢

Teach you how to run two or more MySQL databases at the same time in one system

The reason why the computer runs slowly and how to solve it

1 Introduction to spark Foundation

BUUCTF,Misc:LSB

Bluebridge cup Guoxin Changtian single chip microcomputer -- hardware environment (I)

On my first day at work, this API timeout optimization put me down!

Redis single thread and multi thread
![[SRS] build a specified version of SRS](/img/01/0d2d762e01b304220b8924d20277e3.jpg)
[SRS] build a specified version of SRS

Electronic tube: Literature Research on basic characteristics of 6j1

Programming language (1)
随机推荐
Why use pycharm to run the use case successfully but cannot exit?
Pooling idea: string constant pool, thread pool, database connection pool
JS closure knowledge points essence
Is the account opening of Guotai Junan Securities safe and reliable? How to open Guotai Junan Securities Account
UC Berkeley proposes a multitask framework slip
[dynamic planning] counting garlic customers: the log of garlic King (the longest increasing public subsequence)
DR-AP40X9-A-Qualcomm-IPQ-4019-IPQ-4029-5G-4G-LTE-aluminum-body-dual-band-wifi-router-2.4GHZ-5GHz-QSD
Report on the development status and investment planning trends of China's data center industry Ⓡ 2022 ~ 2028
How PHP gets all method names of objects
Why should enterprises do more application activities?
DR882-Qualcomm-Atheros-QCA9882-2T2R-MIMO-802.11ac-Mini-PCIe-Wi-Fi-Module-5G-high-power
Quick one click batch adding video text watermark and modifying video size simple tutorial
Cognitive fallacy: what is dimensional curse
Analysis report on the development prospect and investment strategy of global and Chinese modular automation systems Ⓟ 2022 ~ 2027
Control loop of program (while loop)
[secretly kill little partner pytorch20 days] - [day3] - [example of text data modeling process]
Team collaborative combat penetration tool CS artifact cobalt strike
2022 free examination questions for safety management personnel of hazardous chemical business units and reexamination examination for safety management personnel of hazardous chemical business units
Farmersworld farmers world, no faith, how to talk about success?
油猴插件