当前位置:网站首页>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 :
边栏推荐
- [Jianzhi offer] 57 And are two numbers of S
- #include<>和#include“”的区别
- Servicemesh mainly solves three pain points
- [shutter] shutter layout component (physicalmodel component)
- 20220702 how do programmers build knowledge systems?
- pip安裝whl文件報錯:ERROR: ... is not a supported wheel on this platform
- 《Just because》阅读感受
- [staff] Sibelius 7.5.1 score software installation (software download | software installation)
- VIM command-t plugin error: unable to load the C extension - VIM command-t plugin error: could not load the C extension
- "Actbert" Baidu & Sydney University of technology proposed actbert to learn the global and local video text representation, which is effective in five video text tasks!
猜你喜欢

How to prevent your jar from being decompiled?
![[zero foundation I] Navicat download link](/img/23/e7808884152eeaf186fe756d6adac6.png)
[zero foundation I] Navicat download link

scrcpy这款软件解决了和同事分享手机屏幕的问题| 社区征文

What "real skills" should a million year old cloud native developer master? Alibaba, Tencent, meituan and byte decrypt together
![[shutter] shutter page Jump (route | navigator | page close)](/img/af/3fb2ca18bcec23a5c0c6897570fb53.gif)
[shutter] shutter page Jump (route | navigator | page close)

PIP version update timeout - download using domestic image

A specially designed loss is used to deal with data sets with unbalanced categories

*C语言期末课程设计*——通讯录管理系统(完整项目+源代码+详细注释)

MySQL learning record (6)

Pip install whl file Error: Error: … Ce n'est pas une roue supportée sur cette plateforme
随机推荐
Service visibility and observability
Basic concepts of image and deep understanding of yuv/rgb
[shutter] shutter layout component (wrap component | expanded component)
【零基础一】Navicat下载链接
[staff] Sibelius 7.5.1 score software installation (software download | software installation)
20220702-程序员如何构建知识体系?
Kubernetes resource object introduction and common commands (4)
MySQL installation failed -gpg verification failed
攻防世界pwn题:Recho
Attack and defense world PWN question: Echo
B.Odd Swap Sort(Codeforces Round #771 (Div. 2))
TinyMCE visual editor adds Baidu map plug-in
A specially designed loss is used to deal with data sets with unbalanced categories
关于PHP-数据库的 数据读取,Trying to get property 'num_rows' of non-object?
Web侧防御指南
Technical solution of vision and manipulator calibration system
D4:非成对图像去雾,基于密度与深度分解的自增强方法(CVPR 2022)
Basic IO interface technology - microcomputer Chapter 7 Notes
【剑指 Offer】56 - I. 数组中数字出现的次数
Three chess games