当前位置:网站首页>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
边栏推荐
- Dart JSON编码器和解码器剖析
- Sustainable service business models
- Sqlalchemy - subquery in a where clause - Sqlalchemy - subquery in a where clause
- Flutter network and data storage framework construction-b1
- 硬盘监控和分析工具:Smartctl
- Latex image rotates with title
- 【LeetCode】【SQL】刷题笔记
- 【数学建模】基于matlab船舶三自由度MMG模型【含Matlab源码 1925期】
- High concurrency Architecture - read write separation
- Verilog HDL continuous assignment statement, process assignment statement, process continuous assignment statement
猜你喜欢
组策略中开机脚本与登录脚本所使用的用户身份
235. 二叉搜索樹的最近公共祖先【lca模板 + 找路徑相同】
[leetcode周赛]第300场——6110. 网格图中递增路径的数目-较难
【学术相关】顶级论文创新点怎么找?中国高校首次获CVPR最佳学生论文奖有感...
leetcode:556. 下一个更大元素 III【模拟 + 尽可能少变更】
Flutter network and data storage framework construction-b1
Unity webgl optimization
Failed to start component [StandardEngine[Catalina]. StandardHost[localhost]. StandardContext
我眼中真正优秀的CTO长啥样
Help change the socket position of PCB part
随机推荐
Transformer T5 model read slowly
Zero length array
Suffix derivation based on query object fields
[academic related] how to find the innovation of top papers? Chinese universities won the CVPR Best Student Thesis Award for the first time
Differential constrained SPFA
记录在模拟器中运行flutter时报的错
Flutter网络和数据存储框架搭建 -b1
Go home early today
【LeetCode】【SQL】刷题笔记
What does a really excellent CTO look like in my eyes
ActiveMQ的基础
Add control at the top of compose lazycolumn
Briefly describe the quantitative analysis system of services
shell 脚本中关于用户输入参数的处理
【学术相关】顶级论文创新点怎么找?中国高校首次获CVPR最佳学生论文奖有感...
Webrtc[41] - Analysis of the establishment process of webrtc transmission channel
High concurrency architecture cache
Php based campus lost and found platform (automatic matching push)
Sqlalchemy - subquery in a where clause - Sqlalchemy - subquery in a where clause
【疾病识别】基于matlab GUI机器视觉肺癌检测系统【含Matlab源码 1922期】