当前位置:网站首页>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];
}
};
边栏推荐
- Web-ui automated testing - the most complete element positioning method
- Talk about several methods of interface optimization
- Kotlin学习快速入门(7)——扩展的妙用
- Golang unit test, mock test and benchmark test
- University of Electronic Science and technology, accounting computerization, spring 20 final exam [standard answer]
- PHP online confusion encryption tutorial sharing + basically no solution
- QT学习日记9——对话框
- Swm32 series Tutorial 4 port mapping and serial port application
- Kubernetes resource object introduction and common commands (4)
- Is AI too slow to design pictures and draw illustrations? 3 sets of practical brushes to save you
猜你喜欢

What is your income level in the country?

POM in idea XML graying solution

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

Talk about several methods of interface optimization

Depth first search of graph

Type conversion, variable
![[RT thread] NXP rt10xx device driver framework -- RTC construction and use](/img/19/91a9d84ba84f81ef125c33eb4007bc.png)
[RT thread] NXP rt10xx device driver framework -- RTC construction and use
![[RT thread] NXP rt10xx device driver framework -- pin construction and use](/img/75/b4f034bfe49409f76e7fd92758804e.png)
[RT thread] NXP rt10xx device driver framework -- pin construction and use

Free data | new library online | cnopendata complete data of China's insurance intermediary outlets

互聯網醫院HIS管理平臺源碼,在線問診,預約掛號 智慧醫院小程序源碼
随机推荐
One brush 148 force deduction hot question-5 longest palindrome substring (m)
Notes on problems -- watching videos on edge will make the screen green
University of Electronic Science and technology, accounting computerization, spring 20 final exam [standard answer]
PS screen printing brush 131, many illustrators have followed suit
大变局!全国房价,跌破万元大关
STM32H7 HAL库SPI DMA发送一直处于busy的解决办法
One brush 142 monotone stack next larger element II (m)
Play with fancy special effects. This AE super kit is for you
kubernetes资源对象介绍及常用命令(三)
Collection of the most beautiful graduation photos in the graduation season, collection of excellent graduation photos
Golang unit test, mock test and benchmark test
Svn full backup svnadmin hotcopy
绝对定位时元素水平垂直居中
Solution to long waiting time of SSH connection to remote host
visual studio “通常每个套接字地址(协议/网络地址/端口)只允许使用一次“
Talk about several methods of interface optimization
RedHat 6.2 配置 Zabbix
2021 ICPC regional competition (Shanghai) g.edge groups (tree DP)
One brush 145 force deduction hot question-2 sum of two numbers (m)
Kubernetes resource object introduction and common commands (4)