当前位置:网站首页>Force buckle 540 A single element in an ordered array
Force buckle 540 A single element in an ordered array
2022-07-02 03:58:00 【Ruthless young Fisherman】
subject
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 .
Example
Input : nums = [1,1,2,3,3,4,4,8,8]
Output : 2
Input : nums = [3,3,7,7,10,11,11]
Output : 10
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/single-element-in-a-sorted-array
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Method 1: An operation
Java Realization
class Solution {
public int singleNonDuplicate(int[] nums) {
int ans = 0;
for (int num : nums) {
ans ^= num;
}
return ans;
}
}
Method 2: Two points search
Java Realization
class Solution {
public int singleNonDuplicate(int[] nums) {
int n = nums.length;
int l = 0, r = n - 1;
while (l < r) {
int mid = l + r >> 1;
if (mid % 2 == 0) {
if (nums[mid] == nums[mid + 1]) l = mid + 1;
else r = mid;
} else {
if (nums[mid] == nums[mid - 1]) l = mid + 1;
else r = mid;
}
}
return nums[l];
}
}
边栏推荐
- Get started with Aurora 8b/10b IP core in one day (5) -- learn from the official routine of framing interface
- 高性能 低功耗Cortex-A53核心板 | i.MX8M Mini
- go 语言命名规范
- 【人员密度检测】基于形态学处理和GRNN网络的人员密度检测matlab仿真
- 微信小程序 - 实现获取手机验证码倒计时 60 秒(手机号+验证码登录功能)
- Interface debugging tool simulates post upload file - apipost
- 蓝桥杯单片机省赛第七届
- Oracle viewing locked tables and unlocking
- Basic syntax of unity script (8) - collaborative program and destruction method
- Vite: scaffold assembly
猜你喜欢
蓝桥杯单片机省赛第十一届
The second game of the 12th provincial single chip microcomputer competition of the Blue Bridge Cup
The first game of the 11th provincial single chip microcomputer competition of the Blue Bridge Cup
How should the team choose the feature branch development mode or trunk development mode?
[mv-3d] - multi view 3D target detection network
滴滴开源DELTA:AI开发者可轻松训练自然语言模型
A thorough understanding of the development of scorecards - the determination of Y (Vintage analysis, rolling rate analysis, etc.)
《动手学深度学习》(二)-- 多层感知机
Suggestions on settlement solution of u standard contract position explosion
It took me only 3 months to jump out of the comfort zone and become an automated test engineer for 5 years
随机推荐
Three ways for programmers to learn PHP easily and put chaos out of order
What kind of interview is more effective?
Welcome the winter vacation multi school league game 2 partial solution (B, C, D, F, G, H)
JVM知识点
Which product of anti-cancer insurance is better?
Interface debugging tool simulates post upload file - apipost
Recently, the weather has been extremely hot, so collect the weather data of Beijing, Shanghai, Guangzhou and Shenzhen last year, and make a visual map
Is the product of cancer prevention medical insurance safe?
蓝桥杯单片机省赛第十二届第二场
Failed to upgrade schema, error: “file does not exist
Hand tear - sort
The 11th Blue Bridge Cup single chip microcomputer provincial competition
初识string+简单用法(二)
《动手学深度学习》(二)-- 多层感知机
蓝桥杯单片机省赛第五届
C language: examples of logical operation and judgment selection structure
go 分支与循环
How about Ping An lifetime cancer insurance?
Object oriented thinking
0 foundation how to learn automated testing? Follow these seven steps step by step and you will succeed