当前位置:网站首页>540. Single element in ordered array
540. Single element in ordered array
2022-07-02 22:17:00 【_ Alkaid_】
difficulty : secondary
Catalog
3、 Extreme situation resolution
2、 Time complexity and Spatial complexity
One 、 Problem description
I'm going to use LeetCode The description above .
Give you an ordered array of integers only , Each of these elements will appear twice , Only one number will appear once . Please find and return the number that only appears once .
The solution you design must meet
Time complexity and
Spatial complexity .
Here is an example :

Tips :
1 <= nums.length <= 1050 <= nums[i] <= 105
Two 、 Ideas
1、 Their thinking
1. Train of thought
The requirements given in the title here are :
Time complexity -
Spatial complexity
Obviously, you need to use Two points search To solve the problem .
The title says Ordered array And Each element appears twice , Only one number will appear once . Then look at the example and we can find : Set the current subscript to x
- In the presence of Unique number Before , When x For even when , It must be Satisfy nums[ x ] = nums[ x + 1 ], if x Is odd , You must be satisfied nums[ x ] = nums[ x - 1 ].
- There is Unique number after , When x For even when , It must be Satisfy nums[ x ] = nums[ x - 1 ], if x Is odd , You must be satisfied nums[ x ] = nums[ x + 1 ].
Therefore, we can proceed according to the above conditions Dichotomous condition The judgment of the , because Will only appear One Unique number , So the array nums It must be the size of Odd number .
Let's assume that the current subscript is x :
- If x Is odd , Then with x The number on the left is equal , It's not equal Then the unique number is mid Left side , equal shows Unique number stay x To the right of .
- If x It's even , Then with x The number on the right is equal , It's not equal Then the unique number is mid Left side , equal Then the unique number is x To the right of .
According to the above conditions , You can write a binary search , What we use here is Exclusive or ( ^ ) To distinguish the current mid Of Parity , It's really clever .
2. Train of thought two
Here's what I think , Search for two elements every time you jump , Until you find nums[ x ] != nums[ x + 1 ] When , that nums[ x ], It must be that The only element .
2、 Extreme case judgment
- For method two : What needs to be noted here is , Judge the condition Cannot let subscript x + 1 Transboundary , Then you can't judge the last element , And can't judge nums size by 1 The situation of .
3、 Extreme situation resolution
- It can be found that if the judgment comes to the end , If you don't find the only element , Then the only element can only be the last ; If there is only one element , Then the only element is also the last element . So here's the solution , Finally, directly return nums[ nums.size() - 1 ] , that will do .
3、 ... and 、 Problem solving
1、 Code implementation
1. Method 1
class Solution {
public:
int singleNonDuplicate(vector<int>& nums) {
int left = 0, right = nums.size() - 1;
while (left < right) {
// Every time I find left And right The number in the middle Judge
int mid = (right - left) / 2 + left;
// Here we use and 1 Exclusive or It's perfect
// because If mid Is odd Then compare with the number on the left , If mid It's even Then compare with the number on the right
// The dichotomy here is : Two numbers are equal Then it must be mid A unique number appears on the right
if (nums[mid] == nums[mid ^ 1]) {
left = mid + 1;
}
// If it's not equal shows The order must have been disrupted on the left .
else {
right = mid;
}
}
return nums[left];
}
}; 
2. Method 2
class Solution {
public:
int singleNonDuplicate(vector<int>& nums) {
int numsLength = nums.size();
for(int i = 0; i < numsLength - 1; i += 2){
if(nums[i] == nums[i+1]){
continue;
}else{
return nums[i];
}
}
return nums[numsLength - 1];
}
};
2、 Time complexity and Spatial complexity
1. Method 1
Time complexity :
Spatial complexity :
2. Method 2
Time complexity :
Spatial complexity :
边栏推荐
- D4: unpaired image defogging, self enhancement method based on density and depth decomposition (CVPR 2022)
- Service visibility and observability
- 【leetcode】1380. Lucky number in matrix
- MySQL learning record (8)
- Riding the wind of "cloud native" and stepping on the wave of "digitalization", new programmer 003 starts pre-sale
- 攻防世界pwn题:Recho
- ServiceMesh主要解决的三大痛点
- APP页面分享口令Rails实现
- The book "new programmer 002" is officially on the market! From "new database era" to "software defined car"
- [Yu Yue education] reference materials of analog electronic technology of Nanjing Institute of information technology
猜你喜欢

MySQL learning record (6)

Etcd Raft 协议

How do I access the kubernetes API?

What is it that makes you tremble? Those without fans can learn
![[shutter] shutter layout component (fractionallysizedbox component | stack layout component | positioned component)](/img/5f/e96baefd9481c496024fed345e31fe.jpg)
[shutter] shutter layout component (fractionallysizedbox component | stack layout component | positioned component)

Ransack组合条件搜索实现

"New programmer 003" was officially launched, and the cloud native and digital practical experience of 30+ companies such as Huawei and Alibaba

CVPR论文解读 | 弱监督的高保真服饰模特生成

Redis distributed lock failure, I can't help but want to burst

PIP version update timeout - download using domestic image
随机推荐
Find objects you can't see! Nankai & Wuhan University & eth proposed sinet for camouflage target detection, and the code has been open source
读博士吧,研究奶牛的那种!鲁汶大学 Livestock Technology 组博士招生,牛奶质量监测...
APP页面分享口令Rails实现
情感计算与理解研究发展概述
kubernetes资源对象介绍及常用命令(四)
Detailed explanation of OSI seven layer model
[shutter] shutter gesture interaction (click event handling | click OnTap | double click | long press | click Cancel | press ontapdown | lift ontapup)
Five message formats of OSPF
100 important knowledge points that SQL must master: using cursors
Daily book - low code you must understand in the era of digital transformation
App page sharing password rails implementation
Service visibility and observability
The book "new programmer 002" is officially on the market! From "new database era" to "software defined car"
[shutter] shutter layout component (wrap component | expanded component)
ArrayList分析2 :Itr、ListIterator以及SubList中的坑
《ActBERT》百度&悉尼科技大学提出ActBERT,学习全局局部视频文本表示,在五个视频-文本任务中有效!
How to center the positioned text horizontally and vertically
LandingSite eBand B1冒烟测试用例
ServiceMesh主要解决的三大痛点
Oriental Aesthetics and software design