当前位置:网站首页>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];
}
};
边栏推荐
- Hongmeng fourth training
- Qt调节Win屏幕亮度和声音大小
- 一位普通程序员一天工作清单
- Internet Hospital his Management Platform source, online Inquiry, appointment Registration Smart Hospital Small program source
- How do large consumer enterprises make digital transformation?
- Rsync远程同步
- Stm32h7 Hal library SPI DMA transmission has been in busy solution
- How to enforce parameters in PowerShell- How do I make parameters mandatory in PowerShell?
- Simple configuration of postfix server
- 简单配置PostFix服务器
猜你喜欢
Thread pool: the most common and error prone component of business code
Free data | new library online | cnopendata complete data of China's insurance intermediary outlets
Hongmeng third training
New library online | cnopendata complete data of Chinese insurance institution outlets
Pools de Threads: les composants les plus courants et les plus sujets aux erreurs du Code d'affaires
PHP online confusion encryption tutorial sharing + basically no solution
How to train mask r-cnn model with your own data
跨境电商:外贸企业做海外社媒营销的优势
SWM32系列教程4-端口映射及串口应用
Cross border e-commerce: advantages of foreign trade enterprises in overseas social media marketing
随机推荐
Take you to API development by hand
POM in idea XML graying solution
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 (special solution form | special solution solving method | special solution example)
Apache service suspended asynchronous acceptex failed
Solution to long waiting time of SSH connection to remote host
Golang unit test, mock test and benchmark test
What is your income level in the country?
一位普通程序员一天工作清单
RDS数据库的监测页面在哪看?
One brush 148 force deduction hot question-5 longest palindrome substring (m)
互联网医院HIS管理平台源码,在线问诊,预约挂号 智慧医院小程序源码
Answer to the homework assessment of advanced English reading (II) of the course examination of Fuzhou Normal University in February 2022
Define a structure fraction to represent a fraction, which is used to represent fractions such as 2/3 and 5/6
AcWing 4489. 最长子序列
How do large consumer enterprises make digital transformation?
One brush 146 force buckle hot question-3 longest substring without repeated characters (m)
Swm32 series Tutorial 4 port mapping and serial port application
How to train mask r-cnn model with your own data
人生还在迷茫?也许这些订阅号里有你需要的答案!