当前位置:网站首页>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
边栏推荐
- Leetcode: 11. Récipient contenant le plus d'eau [double pointeur + cupidité + enlèvement de la plaque la plus courte]
- Suffix derivation based on query object fields
- Yolov3 network model building
- 为什么要做特征的归一化/标准化?
- SQL custom collation
- 2020 intermediate financial management (escort class)
- Sqlalchemy - subquery in a where clause - Sqlalchemy - subquery in a where clause
- High concurrency Architecture - read write separation
- Dynamic planning -- expansion topics
- Chisel tutorial - 06 Phased summary: implement an FIR filter (chisel implements 4-bit FIR filter and parameterized FIR filter)
猜你喜欢

Valentine's Day - make an exclusive digital collection for your lover

The installation path cannot be selected when installing MySQL 8.0.23

SQL: special update operation

Record: MySQL changes the time zone

Think of new ways

【光学】基于matlab涡旋光产生【含Matlab源码 1927期】

User identity used by startup script and login script in group policy

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

东数西算拉动千亿产业,敢啃“硬骨头”的存储厂商才更有机会
![[leetcode weekly race] game 300 - 6110 Number of incremental paths in the grid graph - difficult](/img/8d/0e515af6c17971ddf461e3f3b87c30.png)
[leetcode weekly race] game 300 - 6110 Number of incremental paths in the grid graph - difficult
随机推荐
Streaming media server (16) -- figure out the difference between live broadcast and on-demand
Smart wax therapy machine based on STM32 and smart cloud
Unity2018 to wechat games without pictures
Database creation, addition, deletion, modification and query
[leetcode] [SQL] notes
The way to treat feelings
我们做了一个智能零售结算平台
[mathematical modeling] ship three degree of freedom MMG model based on MATLAB [including Matlab source code 1925]
Record: solve the problem that MySQL is not an internal or external command environment variable
变化是永恒的主题
Why should the gradient be manually cleared before back propagation in pytorch?
平淡的生活里除了有扎破皮肤的刺,还有那些原本让你魂牵梦绕的诗与远方
Max of PHP FPM_ Some misunderstandings of children
组策略中开机脚本与登录脚本所使用的用户身份
High concurrency Architecture - read write separation
【光学】基于matlab介电常数计算【含Matlab源码 1926期】
Help change the socket position of PCB part
我們做了一個智能零售結算平臺
知其然,而知其所以然,JS 对象创建与继承【汇总梳理】
Unity webgl optimization