当前位置:网站首页>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];
}
};
边栏推荐
- Vs code plug-in korofileheader
- Dagong 21 autumn "power plant electrical part" online operation 1 [standard answer] power plant electrical part
- Comparison of kotlin collaboration + retro build network request schemes
- What is the difference between cloud server and cloud virtual machine
- STM32H7 HAL库SPI DMA发送一直处于busy的解决办法
- 【RT-Thread】nxp rt10xx 设备驱动框架之--hwtimer搭建和使用
- [combinatorics] recursive equation (characteristic equation and characteristic root | example of characteristic equation | root formula of monadic quadratic equation)
- Assignment examination questions of advanced English (III) for the course examination of Fujian Normal University in February 2022
- Electronic Science and technology 20th autumn "Microcomputer Principle and application" online assignment 2 [standard answer]
- SVN如何查看修改的文件记录
猜你喜欢
![[try to hack] active detection and concealment technology](/img/43/d48f851268fec566ce0cc83bd9557e.png)
[try to hack] active detection and concealment technology

SWM32系列教程4-端口映射及串口应用

kubernetes资源对象介绍及常用命令(四)

Notes on problems -- watching videos on edge will make the screen green

kubernetes资源对象介绍及常用命令(三)

UE4 official charging resources, with a total price of several thousand

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

Pools de Threads: les composants les plus courants et les plus sujets aux erreurs du Code d'affaires

Tensorboard quick start (pytoch uses tensorboard)

设计电商秒杀
随机推荐
Internet hospital his management platform source code, online consultation, appointment registration smart hospital applet source code
Javescript variable declaration -- VaR, let, const
One brush 148 force deduction hot question-5 longest palindrome substring (m)
Cross border e-commerce: advantages of foreign trade enterprises in overseas social media marketing
Play with fancy special effects. This AE super kit is for you
[combinatorics] recursive equation (special solution example 1 Hannover tower complete solution process | special solution example 2 special solution processing when the characteristic root is 1)
Comparison of kotlin collaboration + retro build network request schemes
一位普通程序员一天工作清单
数仓任务里面 跑SQL任务的时候用的数据库账号是在哪里配置的
Take you to API development by hand
The difference between get and post
Solution to long waiting time of SSH connection to remote host
29:第三章:开发通行证服务:12:开发【获得用户账户信息,接口】;(使用VO类包装查到的数据,以符合接口对返回数据的要求)(在多处都会用到的逻辑,在Controller中可以把其抽成一个共用方法)
AcWing 4489. Longest subsequence
SVN完全备份svnadmin hotcopy
[RT thread] NXP rt10xx device driver framework -- RTC construction and use
Vs2013 has blocked the installer, and ie10 needs to be installed
An example of HP array card troubleshooting
Notes on problems -- watching videos on edge will make the screen green
STM32H7 HAL库SPI DMA发送一直处于busy的解决办法