当前位置:网站首页>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];
}
}
边栏推荐
- js实现在可视区内,文字图片动画效果
- 深潜Kotlin协程(十九):Flow 概述
- MySQL create table
- 105. SAP UI5 Master-Detail 布局模式的联动效果实现明细介绍
- [NLP]—sparse neural network最新工作简述
- Makefile demo
- CVPR 2022 | 大連理工提出自校准照明框架,用於現實場景的微光圖像增强
- Pdf editing tool movavi pdfchef 2022 direct download
- 在写web项目的时候,文件上传用到了smartupload,用了new string()进行转码,但是在数据库中,还是会出现类似扑克的乱码
- eth入门之DAPP
猜你喜欢
Mila、渥太华大学 | 用SE(3)不变去噪距离匹配进行分子几何预训练
毕设-基于SSM宠物领养中心
CVPR 2022 | 大连理工提出自校准照明框架,用于现实场景的微光图像增强
CVPR 2022 | 大連理工提出自校准照明框架,用於現實場景的微光圖像增强
Mila, University of Ottawa | molecular geometry pre training with Se (3) invariant denoising distance matching
Competitive product analysis and writing
pytorch怎么下载?pytorch在哪里下载?
深潜Kotlin协程(十九):Flow 概述
在 .NET 6 项目中使用 Startup.cs
2022 mobile crane driver examination registration and mobile crane driver operation examination question bank
随机推荐
How to execute a swift for in loop in one step- How can I do a Swift for-in loop with a step?
深潜Kotlin协程(二十):构建 Flow
2022 P cylinder filling examination content and P cylinder filling practice examination video
vim 的实用操作
Arlo's thinking about himself
pytorch怎么下载?pytorch在哪里下载?
leetcode:297. 二叉树的序列化与反序列化
[DRM] simple analysis of DRM bridge driver call process
The 10th China Cloud Computing Conference · China Station: looking forward to the trend of science and technology in the next decade
[set theory] set concept and relationship (set represents | number set | set relationship | contains | equality | set relationship property)
The longest subarray length with a positive product of 1567 recorded by leecode
Debug: CD cannot be used in kaggle
[set theory] set concept and relationship (set family | set family examples | multiple sets)
js/ts底层实现双击事件
MySQL timestampdiff interval
DAPP for getting started with eth
2022 electrician (Advanced) examination papers and electrician (Advanced) examination skills
Interface embedded in golang struct
【刷题篇】多数元素(超级水王问题)
[Blue Bridge Road -- bug free code] interpretation of some codes of matrix keyboard