当前位置:网站首页>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
边栏推荐
- Web3 credential network project galaxy is better than nym?
- Random numbers in a long range, is that right- Random number in long range, is this the way?
- A green plug-in that allows you to stay focused, live and work hard
- Getting started with JDBC
- Dynamic planning -- expansion topics
- 【水质预测】基于matlab模糊神经网络水质预测【含Matlab源码 1923期】
- Summary of composition materials for 2020 high-frequency examination center of educational resources
- [free sharing] kotalog diary2022 plan electronic manual ledger
- 达梦数据库的物理备份和还原简解
- Introduction to SSH Remote execution command
猜你喜欢
![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]
![235. The nearest common ancestor of the binary search tree [LCA template + same search path]](/img/f5/f2d244e7f19e9ddeebf070a1d06dce.png)
235. The nearest common ancestor of the binary search tree [LCA template + same search path]
![[leetcode] [SQL] notes](/img/8d/160a03b9176b8ccd8d52f59d4bb47f.png)
[leetcode] [SQL] notes

In addition to the prickles that pierce your skin, there are poems and distant places that originally haunt you in plain life

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

The installation path cannot be selected when installing MySQL 8.0.23
![[optics] dielectric constant calculation based on MATLAB [including Matlab source code 1926]](/img/cb/ee696d5a7d6bef96fe0db89e8478ed.jpg)
[optics] dielectric constant calculation based on MATLAB [including Matlab source code 1926]

leetcode:556. 下一个更大元素 III【模拟 + 尽可能少变更】

【学术相关】顶级论文创新点怎么找?中国高校首次获CVPR最佳学生论文奖有感...

Nous avons fait une plateforme intelligente de règlement de détail
随机推荐
Differential constrained SPFA
[water quality prediction] water quality prediction based on MATLAB Fuzzy Neural Network [including Matlab source code 1923]
Think of new ways
FBI warning: some people use AI to disguise themselves as others for remote interview
The most valuable thing
【学术相关】顶级论文创新点怎么找?中国高校首次获CVPR最佳学生论文奖有感...
EGO Planner代碼解析bspline_optimizer部分(1)
[academic related] how to find the innovation of top papers? Chinese universities won the CVPR Best Student Thesis Award for the first time
Le changement est un thème éternel
[leetcode weekly race] game 300 - 6110 Number of incremental paths in the grid graph - difficult
Web3 credential network project galaxy is better than nym?
Bad mentality leads to different results
leetcode:11. Container with the most water [double pointer + greed + remove the shortest board]
[leetcode] [SQL] notes
我們做了一個智能零售結算平臺
Integrated easy to pay secondary domain name distribution system
php-fpm的max_chindren的一些误区
硬盘监控和分析工具:Smartctl
Briefly describe the quantitative analysis system of services
Latex image rotates with title