当前位置:网站首页>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];
}
}
边栏推荐
- xrandr修改分辨率与刷新率
- 2022 beautician (intermediate) new version test questions and beautician (intermediate) certificate examination
- MySQL field userid comma separated save by userid query
- Wechat applet + Alibaba IOT platform + Hezhou air724ug built with server version system analysis
- nodejs基础:浅聊url和querystring模块
- How to execute a swift for in loop in one step- How can I do a Swift for-in loop with a step?
- 105. Detailed introduction of linkage effect realization of SAP ui5 master detail layout mode
- Analysis of the reason why the server cannot connect remotely
- 105. SAP UI5 Master-Detail 布局模式的联动效果实现明细介绍
- "Final review" 16/32-bit microprocessor (8086) basic register
猜你喜欢

学会pytorch能干什么?

Esp32 series (3): GPIO learning (take simple GPIO input and output, ADC, DAC as examples)

2022 P cylinder filling examination content and P cylinder filling practice examination video

有监督预训练!文本生成又一探索!

Nodejs Foundation: shallow chat URL and querystring module

国产PC系统完成闭环,替代美国软硬件体系的时刻已经到来

JS实现图片懒加载

Makefile demo

pytorch项目怎么跑?

Appium自动化测试框架
随机推荐
Error c2694 "void logger:: log (nvinfer1:: ilogger:: severity, const char *)": rewrite the restrictive exception specification of virtual functions than base class virtual member functions
Mila、渥太华大学 | 用SE(3)不变去噪距离匹配进行分子几何预训练
Xrandr modifier la résolution et le taux de rafraîchissement
Bisher - based on SSM pet adoption center
2022 P cylinder filling examination content and P cylinder filling practice examination video
Fcpx template: sweet memory electronic photo album photo display animation beautiful memory
2022 polymerization process examination questions and polymerization process examination skills
[national programming] [software programming - Lecture Video] [zero foundation introduction to practical application]
Half of 2022 is over, so we must hurry up
mysql字段userid逗号分开保存按userid查询
Write it down once Net travel management background CPU Explosion Analysis
Is pytorch difficult to learn? How to learn pytorch well?
Five elements of user experience
js实现在可视区内,文字图片动画效果
Sklearn data preprocessing
eth入门之简介
Nat. Comm. | 使用Tensor-cell2cell对细胞通讯进行环境感知去卷积
[Apple Photo Album push] IMessage group anchor local push
拆一辆十万元的比亚迪“元”,快来看看里面的有哪些元器件。
在写web项目的时候,文件上传用到了smartupload,用了new string()进行转码,但是在数据库中,还是会出现类似扑克的乱码