当前位置:网站首页>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];
}
}
边栏推荐
- SAP ui5 application development tutorial 105 - detailed introduction to the linkage effect implementation of SAP ui5 master detail layout mode
- MySQL field userid comma separated save by userid query
- 因果AI,下一代可信AI的产业升级新范式?
- 【刷题篇】 找出第 K 小的数对距离
- MPLS setup experiment
- JS实现图片懒加载
- Taking two column waterfall flow as an example, how should we build an array of each column
- The longest subarray length with a positive product of 1567 recorded by leecode
- China Mobile Internet of things oneos and onenet were selected in the list of 2021 Internet of things demonstration projects
- Idea shortcut keys
猜你喜欢

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 =

Wechat applet + Alibaba IOT platform + Hezhou air724ug build a serverless IOT system (III) -- wechat applet is directly connected to Alibaba IOT platform aliiot

Mutex and rwmutex in golang

Nodejs Foundation: shallow chat URL and querystring module

2022 polymerization process examination questions and polymerization process examination skills

2022 Shandong Province safety officer C certificate examination questions and Shandong Province safety officer C certificate simulation examination question bank

Wechat applet + Alibaba IOT platform + Hezhou air724ug built with server version system analysis

Competitive product analysis and writing

Bisher - based on SSM pet adoption center

Esp32 series (3): GPIO learning (take simple GPIO input and output, ADC, DAC as examples)
随机推荐
2022deepbrainchain biweekly report no. 104 (01.16-02.15)
[Apple Push] IMessage group sending condition document (push certificate) development tool pushnotification
[set theory] set concept and relationship (set represents | number set | set relationship | contains | equality | set relationship property)
pytorch怎么下载?pytorch在哪里下载?
Basic MySQL operations
SAP ui5 application development tutorial 105 - detailed introduction to the linkage effect implementation of SAP ui5 master detail layout mode
The 10th China Cloud Computing Conference · China Station: looking forward to the trend of science and technology in the next decade
Deep dive kotlin synergy (19): flow overview
js/ts底层实现双击事件
105. SAP UI5 Master-Detail 布局模式的联动效果实现明细介绍
[daily question] dichotomy - find a single dog (Bushi)
Use of sigaction
2022 P cylinder filling examination content and P cylinder filling practice examination video
Intercept string fixed length to array
[mathematical logic] propositional logic (equivalent calculus | idempotent law | exchange law | combination law | distribution law | De Morgan law | absorption rate | zero law | identity | exclusion l
The latest activation free version of Omni toolbox
[set theory] set concept and relationship (true subset | empty set | complete set | power set | number of set elements | power set steps)
Cnopendata China Customs Statistics
JS realizes lazy loading of pictures
Js/ts bottom implementation double click event