当前位置:网站首页>Leetcode Valentine's Day Special - looking for a single dog
Leetcode Valentine's Day Special - looking for a single dog
2022-07-03 17:24:00 【C+++++++++++++++++++】
List of articles
subject

Topic explanation
This question should have been simple , Is a simple XOR law , But the title requires the use of O(logn) Time complexity , O(1) Spatial complexity , And if direct XOR , Can only be O(n) Time complexity of .
So how to do it ?
This question has two paragraphs , What is dyadic , That is, there can be a dividing point to divide the characteristics into two . Because the data is ordered , So elements with two numbers will be next to each other , And in single af The subscripts of continuous elements on the left will have the following rules : In two adjacent identical elements , The subscript of the first element will be even , The second is odd . The subscripts of continuous elements on the right will have the following rules : In two adjacent identical elements , The first element subscript is an odd number , The second is even .
According to the above two paragraphs, you can quickly build a binary version of the code !
Solution code
The binary version is not simplified
class Solution {
public:
int singleNonDuplicate(vector<int>& nums) {
int l=0,r=nums.size()-1;
int maxr = r;
int mid;
while(l<r){
mid = (l+r)/2;
if(mid<maxr&&nums[mid+1]==nums[mid]){
if(mid%2==0){
l = mid+2;
}else{
r = mid;
}
}else if(mid>0&&nums[mid-1]==nums[mid]){
if((mid-1)%2==0){
l = mid+1;
}else{
r = mid;
}
}else{
return nums[mid];
}
}
return nums[l];
}
};
Simplified binary version
class Solution {
public:
int singleNonDuplicate(vector<int>& nums) {
int low = 0, high = nums.size() - 1;
while (low < high) {
int mid = (high - low) / 2 + low;
if (nums[mid] == nums[mid ^ 1]) {
low = mid + 1;
} else {
high = mid;
}
}
return nums[low];
}
};
边栏推荐
- 绝对定位时元素水平垂直居中
- AcWing 4489. 最长子序列
- C language string inversion
- 27. Input 3 integers and output them in descending order. Pointer method is required.
- Cross border e-commerce: advantages of foreign trade enterprises in overseas social media marketing
- [UE4] brush Arctic pack high quality Arctic terrain pack
- SVN如何查看修改的文件记录
- [error reporting] omp: error 15: initializing libiomp5md dll, but found libiomp5md. dll already initialized.
- 网络硬盘NFS的安装与配置
- Simple use of unity pen XR grab
猜你喜欢

QT adjust win screen brightness and sound size

Redis:关于列表List类型数据的操作命令

Golang unit test, mock test and benchmark test

Thread pool: the most common and error prone component of business code

Take you to API development by hand

国内如何购买Google Colab会员

【RT-Thread】nxp rt10xx 设备驱动框架之--rtc搭建和使用

Play with fancy special effects. This AE super kit is for you

QT学习日记9——对话框

How to train mask r-cnn model with your own data
随机推荐
企业级自定义表单引擎解决方案(十一)--表单规则引擎1
【JokerのZYNQ7020】DDS_ Compiler。
Visual studio "usually, each socket address (Protocol / network address / port) can only be used once“
Play with fancy special effects. This AE super kit is for you
Great changes! National housing prices fell below the 10000 yuan mark
Select 3 fcpx plug-ins. Come and see if you like them
Wechat applet for the first time
Luogu: p2685 [tjoi2012] Bridge
SSH连接远程主机等待时间过长的解决方法
SWM32系列教程4-端口映射及串口应用
[combinatorics] recursive equation (special solution form | special solution solving method | special solution example)
设计电商秒杀
1164 Good in C
国内如何购买Google Colab会员
[combinatorics] recursive equation (example of solving recursive equation without multiple roots | complete process of solving recursive equation without multiple roots)
Tensorboard quick start (pytoch uses tensorboard)
AcWing 3438. Number system conversion
毕业总结
Answer to the homework assessment of advanced English reading (II) of the course examination of Fuzhou Normal University in February 2022
Notes on problems -- watching videos on edge will make the screen green