当前位置:网站首页>Two points -leetcode-540 A single element in an ordered array
Two points -leetcode-540 A single element in an ordered array
2022-07-03 04:10:00 【pospre】
540. A single element in an ordered array
Title Description
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 .

Tips :
- 1 < = n u m s . l e n g t h < = 1 0 5 1 <= nums.length <= 10^5 1<=nums.length<=105
- 0 < = n u m s [ i ] < = 1 0 5 0 <= nums[i] <= 10^5 0<=nums[i]<=105
violence
Pure violence is the use of a map, Record the number of times each element appears . then , Take the element that only appears once as the result . like that The space complexity is O ( n ) O(n) O(n), The following method is only a small optimization in space , In terms of time, it is still a violent solution …
Ideas
- Because the title has required the array to be ordered , And “ Each element appears twice , Only one number will appear once ”
- therefore , You can go from front to back , Scan every two , Until you find a group of neighbors 2 Elements Unequal until .
class Solution {
public int singleNonDuplicate(int[] nums) {
int n = nums.length;
if (n == 1) return nums[0];
int i = 1;
for ( ; i < n; i += 2) {
if (nums[i] != nums[i - 1]) break;
}
System.out.println("i = " + i);
return nums[i - 1];
}
}
- Time complexity : O ( n ) O(n) O(n)
- Spatial complexity : O ( 1 ) O(1) O(1)
The title requires time in O ( l o n g n ) O(longn) O(longn), This solution is not ok

An operation
Ideas :
- It's a question “ Each element appears twice , Only one number will appear once ”
- This is exactly in line with “ Exclusive or ” Thought , So you can go through “ Exclusive or ” solve …
class Solution {
public int singleNonDuplicate(int[] nums) {
int n = nums.length;
int res = 0;
for (int i = 0; i < n; i++) {
// Exclusive or operation
res ^= nums[i];
}
return res;
}
}
- Time complexity : O ( n ) O(n) O(n)
- Spatial complexity : O ( 1 ) O(1) O(1)

Two points
ps: Today is Valentine's Day , Two points for the dog (wo) It's too mixed
Reference resources : Brother Tong - Answer key
class Solution {
public int singleNonDuplicate(int[] nums) {
// Two points search
int left = 0, right = nums.length - 1;
while (left < right) {
int mid = (left + right) / 2;
if (mid % 2 == 0) {
if (nums[mid] == nums[mid + 1]) {
left = mid + 1;
} else {
right = mid;
}
} else {
if (nums[mid] == nums[mid - 1]) {
left = mid + 1;
} else {
right = mid;
}
}
}
return nums[right];
}
}
边栏推荐
- pytorch难学吗?如何学好pytorch?
- Cnopendata China Customs Statistics
- 2022deepbrainchain biweekly report no. 104 (01.16-02.15)
- [mathematical logic] propositional logic (equivalent calculus | idempotent law | exchange law | combination law | distribution law | De Morgan law | absorption rate | zero law | identity | exclusion l
- Interface embedded in golang struct
- Interaction free shell programming
- JS realizes the animation effect of text and pictures in the visual area
- [brush questions] connected with rainwater (one dimension)
- Bisher - based on SSM pet adoption center
- [DRM] simple analysis of DRM bridge driver call process
猜你喜欢

2022 polymerization process examination questions and polymerization process examination skills

Cnopendata China Customs Statistics

pytorch开源吗?

『期末复习』16/32位微处理器(8086)基本寄存器
![[NLP]—sparse neural network最新工作简述](/img/65/35ae0137f4030bdb2b0ab9acd85e16.png)
[NLP]—sparse neural network最新工作简述

pytorch是什么?pytorch是一个软件吗?

Daily question - ugly number

2022 tea master (intermediate) examination questions and analysis and tea master (intermediate) practical examination video

毕设-基于SSM宠物领养中心

SAP ui5 application development tutorial 105 - detailed introduction to the linkage effect implementation of SAP ui5 master detail layout mode
随机推荐
xrandr修改分辨率與刷新率
Application of I2C protocol of STM32F103 (read and write EEPROM)
2022-07-02: what is the output of the following go language code? A: Compilation error; B:Panic; C:NaN。 package main import “fmt“ func main() { var a =
Deep dive kotlin synergy (20): build flow
MySQL create table
深潜Kotlin协程(二十):构建 Flow
[NLP]—sparse neural network最新工作简述
类的基础语法
Interface in TS
树莓派如何连接WiFi
[home push IMessage] software installation virtual host rental tothebuddy delay
Nat. Comm. | use tensor cell2cell to deconvolute cell communication with environmental awareness
有监督预训练!文本生成又一探索!
Debug: CD cannot be used in kaggle
Which code editor is easy to use? Code editing software recommendation
Esp32 series (3): GPIO learning (take simple GPIO input and output, ADC, DAC as examples)
C language hashtable/hashset library summary
2022 tea master (intermediate) examination questions and analysis and tea master (intermediate) practical examination video
What is pytorch? Is pytorch a software?
[no title] 2022 chlorination process examination content and free chlorination process examination questions