当前位置:网站首页>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];
}
};
边栏推荐
- kubernetes资源对象介绍及常用命令(四)
- Javescript variable declaration -- VaR, let, const
- Apache service suspended asynchronous acceptex failed
- Examination questions for the assignment of selected readings of British and American Literature in the course examination of Fujian Normal University in February 2022
- [combinatorics] recursive equation (the problem of solving recursive equation with multiple roots | the problem is raised)
- Play with fancy special effects. This AE super kit is for you
- Redis:关于列表List类型数据的操作命令
- SSH连接远程主机等待时间过长的解决方法
- Is AI too slow to design pictures and draw illustrations? 3 sets of practical brushes to save you
- 互联网医院HIS管理平台源码,在线问诊,预约挂号 智慧医院小程序源码
猜你喜欢

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

TensorBoard快速入门(Pytorch使用TensorBoard)

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

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

Thread pool: the most common and error prone component of business code
![[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

QT学习日记9——对话框

【JokerのZYNQ7020】DDS_ Compiler。

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

鸿蒙第三次培训
随机推荐
POM in idea XML graying solution
Free data | new library online | cnopendata complete data of China's insurance intermediary outlets
2021 ICPC regional competition (Shanghai) g.edge groups (tree DP)
Play with fancy special effects. This AE super kit is for you
Cross border e-commerce: advantages of foreign trade enterprises in overseas social media marketing
自动渗透测试工具核心功能简述
Redis:关于列表List类型数据的操作命令
New library online | cnopendata complete data of Chinese insurance institution outlets
Open vsftpd port under iptables firewall
Hongmeng fourth training
【JokerのZYNQ7020】DDS_ Compiler。
[RT thread] NXP rt10xx device driver framework -- pin construction and use
在iptables防火墙下开启vsftpd的端口
Qt调节Win屏幕亮度和声音大小
How to train mask r-cnn model with your own data
Apache service suspended asynchronous acceptex failed
鸿蒙第四次培训
鸿蒙第三次培训
A day's work list of an ordinary programmer
C language modifies files by line