当前位置:网站首页>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];
}
};
边栏推荐
- Comparison of kotlin collaboration + retro build network request schemes
- Apache服务挂起Asynchronous AcceptEx failed.
- vs code 插件 koroFileHeader
- What is the difference between cloud server and cloud virtual machine
- TensorBoard快速入门(Pytorch使用TensorBoard)
- SVN完全备份svnadmin hotcopy
- One brush 144 force deduction hot question-1 sum of two numbers (E)
- Vs code plug-in korofileheader
- kubernetes资源对象介绍及常用命令(五)-(NFS&PV&PVC)
- 自动渗透测试工具核心功能简述
猜你喜欢
新库上线 | CnOpenData中国保险机构网点全集数据
互联网医院HIS管理平台源码,在线问诊,预约挂号 智慧医院小程序源码
Internet hospital his management platform source code, online consultation, appointment registration smart hospital applet source code
Tensorboard quick start (pytoch uses tensorboard)
【RT-Thread】nxp rt10xx 设备驱动框架之--rtc搭建和使用
One brush 149 force deduction hot question-10 regular expression matching (H)
1164 Good in C
How to purchase Google colab members in China
The largest matrix (H) in a brush 143 monotone stack 84 histogram
Type conversion, variable
随机推荐
鸿蒙第三次培训
SVN如何查看修改的文件记录
Assignment examination questions of advanced English (III) for the course examination of Fujian Normal University in February 2022
【RT-Thread】nxp rt10xx 设备驱动框架之--Pin搭建和使用
LeetCode13.罗马数字转整数(三种解法)
问题随记 —— 在 edge 上看视频会绿屏
visual studio “通常每个套接字地址(协议/网络地址/端口)只允许使用一次“
What is the difference between cloud server and cloud virtual machine
Tensorboard quick start (pytoch uses tensorboard)
University of Electronic Science and technology, accounting computerization, spring 20 final exam [standard answer]
kubernetes资源对象介绍及常用命令(三)
1164 Good in C
vs2013已阻止安装程序,需安装IE10
新库上线 | CnOpenData中国保险机构网点全集数据
Collection of the most beautiful graduation photos in the graduation season, collection of excellent graduation photos
Online assignment 3 of mobile Internet technology in the 20th autumn of electronic technology [standard answer]
Hongmeng fourth training
Installation and configuration of network hard disk NFS
数仓任务里面 跑SQL任务的时候用的数据库账号是在哪里配置的
2021 ICPC regional competition (Shanghai) g.edge groups (tree DP)