当前位置:网站首页>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];
}
}
边栏推荐
- 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
- Finally got byte offer. The 25-year-old inexperienced perception of software testing is written to you who are still confused
- The first practical project of software tester: web side (video tutorial + document + use case library)
- Basic syntax of unity script (7) - member variables and instantiation
- Wpviewpdf Delphi and Net PDF viewing component
- Pandora IOT development board learning (HAL Library) - Experiment 2 buzzer experiment (learning notes)
- Analysis of the overall design principle of Nacos configuration center (persistence, clustering, information synchronization)
- 集成底座方案演示说明
- Delete the code you wrote? Sentenced to 10 months!
- WPViewPDF Delphi 和 .NET 的 PDF 查看组件
猜你喜欢
0 foundation how to learn automated testing? Follow these seven steps step by step and you will succeed
Installation and use of blue lake
蓝桥杯单片机省赛第十一届第二场
初识P4语言
Jetpack's livedata extension mediatorlivedata
The first practical project of software tester: web side (video tutorial + document + use case library)
Analysis of the overall design principle of Nacos configuration center (persistence, clustering, information synchronization)
[ibdfe] matlab simulation of frequency domain equalization based on ibdfe
2022-07-01: at the annual meeting of a company, everyone is going to play a game of giving bonuses. There are a total of N employees. Each employee has construction points and trouble points. They nee
蓝桥杯单片机省赛第八届
随机推荐
u本位合约爆仓清算解决方案建议
Get started with Aurora 8b/10b IP core in one day (5) -- learn from the official routine of framing interface
pip 安装第三方库
It took me only 3 months to jump out of the comfort zone and become an automated test engineer for 5 years
Didi open source Delta: AI developers can easily train natural language models
【leetcode】34. Find the first and last positions of elements in a sorted array
Interface debugging tool simulates post upload file - apipost
0 foundation how to learn automated testing? Follow these seven steps step by step and you will succeed
蓝桥杯单片机省赛第六届
[mv-3d] - multi view 3D target detection network
树莓派GPIO引脚控制红绿灯与轰鸣器
The 11th Blue Bridge Cup single chip microcomputer provincial competition
Use of go package
Homework in Chapter 3 of slam course of dark blue vision -- derivative application of T6 common functions
Monkey测试
【小技巧】使用matlab GUI以对话框模式读取文件
SQL Yiwen get window function
Finally got byte offer. The 25-year-old inexperienced perception of software testing is written to you who are still confused
Go language naming specification
Delete the code you wrote? Sentenced to 10 months!