当前位置:网站首页>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];
}
};
边栏推荐
- Take you to API development by hand
- Hongmeng fourth training
- 一位普通程序员一天工作清单
- 【JokerのZYNQ7020】DDS_ Compiler。
- Dagong 21 autumn "power plant electrical part" online operation 1 [standard answer] power plant electrical part
- 鸿蒙第四次培训
- 国内如何购买Google Colab会员
- [RT thread] NXP rt10xx device driver framework -- Audio construction and use
- kubernetes资源对象介绍及常用命令(五)-(NFS&PV&PVC)
- SWM32系列教程4-端口映射及串口应用
猜你喜欢

Life is still confused? Maybe these subscription numbers have the answers you need!

鸿蒙第四次培训
![29: Chapter 3: develop Passport Service: 12: develop [obtain user account information, interface]; (use VO class to package the found data to meet the requirements of the interface for the returned da](/img/1c/c655c8232de1c56203873dcf171f45.png)
29: Chapter 3: develop Passport Service: 12: develop [obtain user account information, interface]; (use VO class to package the found data to meet the requirements of the interface for the returned da

Is AI too slow to design pictures and draw illustrations? 3 sets of practical brushes to save you

Golang单元测试、Mock测试以及基准测试

Design e-commerce spike
![[RT thread] NXP rt10xx device driver framework -- Audio construction and use](/img/85/32a83eaa4b7f5b30d4d7c4f4c32791.png)
[RT thread] NXP rt10xx device driver framework -- Audio construction and use

Great changes! National housing prices fell below the 10000 yuan mark

Hongmeng third training

鸿蒙第三次培训
随机推荐
SVN如何查看修改的文件记录
Golang unit test, mock test and benchmark test
Internet hospital his management platform source code, online consultation, appointment registration smart hospital applet source code
One brush 145 force deduction hot question-2 sum of two numbers (m)
简单配置PostFix服务器
AcWing 3438. Number system conversion
Redis:关于列表List类型数据的操作命令
Depth first search of graph
鸿蒙第三次培训
[combinatorics] recursive equation (general solution structure of recursive equation with multiple roots | linear independent solution | general solution with multiple roots | solution example of recu
How SVN views modified file records
大变局!全国房价,跌破万元大关
新库上线 | CnOpenData中国观鸟记录数据
Kotlin learning quick start (7) -- wonderful use of expansion
Vs2013 has blocked the installer, and ie10 needs to be installed
Type conversion, variable
List of financial products in 2022
Apache service suspended asynchronous acceptex failed
Great changes! National housing prices fell below the 10000 yuan mark
What is the difference between cloud server and cloud virtual machine