当前位置:网站首页>540. Single element in ordered array
540. Single element in ordered array
2022-07-03 22:21:00 【Phoenix_ ZengHao】
subject
540. A single element in an ordered array
The main idea of the topic
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 O(log n) Time complexity and O(1) Spatial complexity .
Examples

Data scale

Ideas 1
Consider the simplest way first : Because other elements appear twice , Only one element will appear once , Then the XOR value of the number that appears twice is 0, Then the XOR value of all numbers is the value of the element that appears only once .
Time complexity O ( n ) O(n) O(n)
Code 1
class Solution {
public:
int singleNonDuplicate(vector<int>& nums) {
int ans=0;
for(int i=0;i<nums.size();i++){
ans^=nums[i];
}
return ans;
}
};
Ideas 2
Consider how to O ( l o g n ) O(logn) O(logn) Realization : The subscript corresponding to the first of the paired elements must be even , The subscript corresponding to the second of the paired elements must be odd . Then consider the existence of a single element , If the subscript of a single element is x x x, So subscript x x x Before ( On the left ) The position of still satisfies the above conclusion , And subscripts x x x after ( On the right ) Due to x x x Insertion , Cause the conclusion to flip . So you can use dichotomy to judge contraction .
Time complexity O ( l o g n ) O(logn) O(logn)
Code 2
class Solution {
public:
int singleNonDuplicate(vector<int>& nums) {
int l=0,r=nums.size()-1,ans=0;
while(l<r){
int mid=(l+r)/2;
if(nums[mid]==nums[mid^1]){
l=mid+1;
}
else{
r=mid;
ans=mid;
}
}
return nums[r];
}
};
边栏推荐
- Programming language (1)
- Data consistency between redis and database
- The difference between SRAM and DRAM
- Tkinter Huarong Road 4x4 tutorial III
- Preliminary analysis of smart microwave radar module
- Rest reference
- Awk getting started to proficient series - awk quick start
- SDNU_ ACM_ ICPC_ 2022_ Winter_ Practice_ 4th [individual]
- Uboot migration
- Blue Bridge Cup Guoxin Changtian single chip microcomputer -- led lamp module (V)
猜你喜欢

Leetcode: a single element in an ordered array

Programming language (2)
![[template summary] - binary search tree BST - Basics](/img/78/782129abea06c8e3bef69a121cbce0.jpg)
[template summary] - binary search tree BST - Basics

Yyds dry goods inventory Spring Festival "make" your own fireworks
![[Android reverse] application data directory (files data directory | lib application built-in so dynamic library directory | databases SQLite3 database directory | cache directory)](/img/b8/e2a59772d009b6ee262fb4807f2cd2.jpg)
[Android reverse] application data directory (files data directory | lib application built-in so dynamic library directory | databases SQLite3 database directory | cache directory)

Asynchronous artifact: implementation principle and usage scenario of completable future

Unique in China! Alibaba cloud container service enters the Forrester leader quadrant

Blue Bridge Cup Guoxin Changtian single chip microcomputer -- software environment (II)

Farmersworld farmers world, no faith, how to talk about success?

The overseas listing of Shangmei group received feedback, and brands such as Han Shu and Yiye have been notified for many times and received attention
随机推荐
Label coco format data and format data in the upper left corner and lower right corner are mutually converted
Development mode and Prospect of China's IT training industry strategic planning trend report Ⓣ 2022 ~ 2028
China's Call Center Industry 14th five year plan direction and operation analysis report Ⓔ 2022 ~ 2028
WFC900M-Network_ Card/Qualcomm-Atheros-AR9582-2T-2R-MIMO-802.11-N-900M-high-power-Mini-PCIe-Wi-Fi-Mod
[Android reverse] application data directory (files data directory | lib application built-in so dynamic library directory | databases SQLite3 database directory | cache directory)
On my first day at work, this API timeout optimization put me down!
[secretly kill little partner pytorch20 days] - [day3] - [example of text data modeling process]
320. Energy Necklace (ring, interval DP)
[sg function] lightoj Partitioning Game
China's coal industry investment strategic planning future production and marketing demand forecast report Ⓘ 2022 ~ 2028
Yyds dry inventory Chapter 4 of getting started with MySQL: data types that can be stored in the data table
油猴插件
[golang] leetcode intermediate - alphabetic combination of island number and phone number
Code in keil5 -- use the code formatting tool astyle (plug-in)
Getting started with DOM
Investment analysis and prospect trend prediction report of China's boron nitride industry Ⓨ 2022 ~ 2028
Kali2021.4a build PWN environment
The overseas listing of Shangmei group received feedback, and brands such as Han Shu and Yiye have been notified for many times and received attention
SDNU_ ACM_ ICPC_ 2022_ Winter_ Practice_ 4th [individual]
Luogu deep foundation part 1 Introduction to language Chapter 6 string and file operation