当前位置:网站首页>2022.2.14 Li Kou - daily question - single element in an ordered array
2022.2.14 Li Kou - daily question - single element in an ordered array
2022-07-03 19:08:00 【It's too powerful to sell】
Title Description :
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 :

Method 1 ( An operation ):
class Solution {
public:
int singleNonDuplicate(vector<int>& nums) {
int a = 0;
for (int num : nums)
{
a ^= num;
}
return a;
}
};The time complexity is O(n), Not strictly meet the requirements of the topic
Method 2 ( Two points search ):
class Solution {
public:
int singleNonDuplicate(vector<int>& nums) {
int left = 0, right = nums.size() - 1;
while (left < right)
{
int mid = (left + right) / 2;
if (nums[mid] == nums[mid ^ 1]) left = mid + 1;
else right = mid;
}
return nums[left];
}
};This is the idea :
Suppose the subscript of the single element to be found is x, Because other elements appear in pairs , therefore x The number of elements on both sides is even . about x Subscript of the left element of y:
· If y It's even , Then meet nums[y] = nums[y + 1]
· If y Is odd , Then meet nums[y] = nums[y - 1]
about x The subscript of the right element of z On the contrary .
therefore , When using dichotomy , Every time the subscript of the obtained intermediate element mid:
· If mid It's even , Then compare nums[mid] and nums[mid + 1] Whether it is equal or not
· If mid Is odd , Then compare nums[mid] and nums[mid - 1] Whether it is equal or not
In both cases , If equal , Explain subscript mid be located x Left side ,mid < x, At this time left = mid + 1; If it's not equal , explain mid be located x On the right or mid It happens to be x, At this time right = mid.
Here is another clever place :
· When mid When it's even ,mid + 1 = mid ^ 1
· When mid It's an odd number ,mid - 1 = mid ^ 1
So you can use mid ^ 1 To replace the above mid In two cases
边栏推荐
- Change is the eternal theme
- During MySQL installation, the download interface is empty, and the components to be downloaded are not displayed. MySQL installer 8.0.28.0 download interface is empty solution
- Pytorch introduction to deep learning practice notes 13- advanced chapter of cyclic neural network - Classification
- [free sharing] kotalog diary2022 plan electronic manual ledger
- __ Weak and__ The difference between blocks
- Hard disk monitoring and analysis tool: smartctl
- Integrated easy to pay secondary domain name distribution system
- Thinking about festivals
- Failed to start component [StandardEngine[Catalina]. StandardHost[localhost]. StandardContext
- Does SQL always report foreign key errors when creating tables?
猜你喜欢

【Proteus仿真】用24C04与1602LCD设计的简易加密电子密码锁

leetcode:556. 下一个更大元素 III【模拟 + 尽可能少变更】
![235. Ancêtre public le plus proche de l'arbre de recherche binaire [modèle LCA + même chemin de recherche]](/img/f5/f2d244e7f19e9ddeebf070a1d06dce.png)
235. Ancêtre public le plus proche de l'arbre de recherche binaire [modèle LCA + même chemin de recherche]

SQL: special update operation

Record: solve the problem that MySQL is not an internal or external command environment variable

A green plug-in that allows you to stay focused, live and work hard

Summary of composition materials for 2020 high-frequency examination center of educational resources
![[proteus simulation] a simple encrypted electronic password lock designed with 24C04 and 1602LCD](/img/51/209e35e0b94a51b3b406a184459475.png)
[proteus simulation] a simple encrypted electronic password lock designed with 24C04 and 1602LCD
![Free hand account sharing in September - [cream Nebula]](/img/4f/fec31778a56886585e35be87885452.jpg)
Free hand account sharing in September - [cream Nebula]

22.2.14 -- station B login with code -for circular list form - 'no attribute' - 'needs to be in path selenium screenshot deviation -crop clipping error -bytesio(), etc
随机推荐
Reading a line from ifstream into a string variable
FBI警告:有人利用AI换脸冒充他人身份进行远程面试
Yolov3 network model building
High concurrency Architecture - read write separation
我們做了一個智能零售結算平臺
The online customer service system developed by PHP is fully open source without encryption, and supports wechat customer service docking
The installation path cannot be selected when installing MySQL 8.0.23
我眼中真正优秀的CTO长啥样
Processing of user input parameters in shell script
DriveSeg:动态驾驶场景分割数据集
Su embedded training - Day10
OSPF - detailed explanation of stub area and full stub area
Free sharing | linefriends hand account inner page | horizontal grid | not for sale
Hard disk monitoring and analysis tool: smartctl
[water quality prediction] water quality prediction based on MATLAB Fuzzy Neural Network [including Matlab source code 1923]
Analyse du Code du planificateur ego bspline Section Optimizer (1)
Flutter network and data storage framework construction-b1
math_ Taylor formula
Web3 credential network project galaxy is better than nym?
php-fpm的max_chindren的一些误区