当前位置:网站首页>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];
}
}
边栏推荐
- [mathematical logic] predicate logic (judge whether the first-order predicate logic formula is true or false | explain | example | predicate logic formula type | forever true | forever false | satisfi
- Esp32 series (3): GPIO learning (take simple GPIO input and output, ADC, DAC as examples)
- 2022 mobile crane driver examination registration and mobile crane driver operation examination question bank
- 用户体验五要素
- 【刷题篇】接雨水(一维)
- The 10th China Cloud Computing Conference · China Station: looking forward to the trend of science and technology in the next decade
- x Problem B
- The time has come for the domestic PC system to complete the closed loop and replace the American software and hardware system
- JS实现图片懒加载
- [Apple Push] IMessage group sending condition document (push certificate) development tool pushnotification
猜你喜欢

pytorch怎么下载?pytorch在哪里下载?

在 .NET 6 项目中使用 Startup.cs

What can learning pytorch do?

Bisher - based on SSM pet adoption center
![[Apple Photo Album push] IMessage group anchor local push](/img/a7/6a27d646ecba0d7c93f8dac38492a2.jpg)
[Apple Photo Album push] IMessage group anchor local push

IPv6 foundation construction experiment

How to download pytorch? Where can I download pytorch?

JMeter starts from zero (III) -- simple use of regular expressions

CVPR 2022 | 大連理工提出自校准照明框架,用於現實場景的微光圖像增强

Mila、渥太华大学 | 用SE(3)不变去噪距离匹配进行分子几何预训练
随机推荐
[mathematical logic] predicate logic (toe normal form | toe normal form conversion method | basic equivalence of predicate logic | name changing rules | predicate logic reasoning law)
[mathematical logic] propositional logic (judgment of the correctness of propositional logic reasoning | formal structure is eternal truth - equivalent calculus | deduction from premise - logical reas
leetcode:297. 二叉树的序列化与反序列化
[NLP]—sparse neural network最新工作简述
JS realizes the animation effect of text and pictures in the visual area
【毕业季·进击的技术er】职场人的自白
Dynamic programming: Longest palindrome substring and subsequence
MySQL field userid comma separated save by userid query
eth入门之DAPP
Appium automated testing framework
深潜Kotlin协程(二十):构建 Flow
[Blue Bridge Road -- bug free code] interpretation of some codes of matrix keyboard
Is it better to speculate in the short term or the medium and long term? Comparative analysis of differences
Deep dive kotlin synergy (19): flow overview
Xrandr modify resolution and refresh rate
2022 P cylinder filling examination content and P cylinder filling practice examination video
竞品分析撰写
Reflection and planning of a sophomore majoring in electronic information engineering
The latest activation free version of Omni toolbox
Mutex and rwmutex in golang