当前位置:网站首页>The force deduction method summarizes the single elements in the 540 ordered array
The force deduction method summarizes the single elements in the 540 ordered array
2022-07-04 00:58:00 【Lost summer】
Original link : Power button
describe :
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 1:
Input : nums = [1,1,2,3,3,4,4,8,8]
Output : 2
Example 2:
Input : nums = [3,3,7,7,10,11,11]
Output : 10
Tips :
1 <= nums.length <= 105
0 <= nums[i] <= 105
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 .
Their thinking :
* Their thinking : * logN The complexity of must be binary search , Judgment in middle Before or after . If even and odd digits are equal , Then after , Otherwise, it was before .
Code :
public class Solution540 {
public int singleNonDuplicate(int[] nums) {
if (nums.length == 1) {
return nums[0];
}
int left = 0;
int right = nums.length - 1;
int middle;
int target = 0;
while (left <= right) {
middle = (left + right) / 2;
if (middle % 2 == 0) {
if (middle < nums.length - 1 && nums[middle] == nums[middle + 1]) {
left = middle + 1;
} else {
right = middle - 1;
target = middle;
}
} else {
if (nums[middle] == nums[middle + 1]) {
right = middle - 1;
target = middle;
} else {
left = middle + 1;
}
}
}
return nums[target];
}
}边栏推荐
- Leetcode 121 best time to buy and sell stock (simple)
- What is regression testing? Talk about regression testing in the eyes of Ali Test Engineers
- Att & CK actual combat series - red team actual combat - V
- 使用dnSpy对无源码EXE或DLL进行反编译并且修改
- Optimization of for loop
- STM32 key light
- It's OK to have hands-on 8 - project construction details 3-jenkins' parametric construction
- 功能:求出菲波那契数列的前一项与后一项之比的极限的 近似值。例如:当误差为0.0001时,函数值为0.618056。
- swagger中响应参数为Boolean或是integer如何设置响应描述信息
- 打印菱形图案
猜你喜欢

In the process of seeking human intelligent AI, meta bet on self supervised learning

Function: find the sum of the elements on the main and sub diagonal of the matrix with 5 rows and 5 columns. Note that the elements where the two diagonals intersect are added only once. For example,

Introduction to unity shader essentials reading notes Chapter III unity shader Foundation

Function: find the approximate value of the limit of the ratio of the former term to the latter term of Fibonacci sequence. For example, when the error is 0.0001, the function value is 0.618056.

How to set the response description information when the response parameter in swagger is Boolean or integer

Sequence list and linked list

老姜的特点
![[common error] UART cannot receive data error](/img/77/6ba56ce6e64beeb73a77d04af5bd0f.jpg)
[common error] UART cannot receive data error

“疫”起坚守 保障数据中台服务“不打烊”

Att & CK actual combat series - red team actual combat - V
随机推荐
[error record] configure NDK header file path in Visual Studio (three header file paths of NDK | ASM header file path selection related to CPU architecture)
“疫”起坚守 保障数据中台服务“不打烊”
功能:求5行5列矩阵的主、副对角线上元素之和。注意, 两条对角线相交的元素只加一次。例如:主函数中给出的矩阵的两条对角线的和为45。
HR disgusted interview behavior
Severity code description the project file line prohibits the display of status error c4996 fopen ('fscanf ', StrCmp): this function or variable may be unsafe The most comprehensive solution
1-redis architecture design to use scenarios - four deployment and operation modes (Part 1)
Query efficiency increased by 10 times! Three optimization schemes to help you solve the deep paging problem of MySQL
[software testing] you haven't mastered these real interview questions of big companies?
Five high-frequency questions were selected from the 200 questions raised by 3000 test engineers
Oracle database knowledge points that cannot be learned (III)
使用dnSpy对无源码EXE或DLL进行反编译并且修改
[common error] UART cannot receive data error
老姜的特点
MySQL winter vacation self-study 2022 12 (2)
不得不会的Oracle数据库知识点(一)
Oracle database knowledge points that cannot be learned (II)
Oracle database knowledge points (IV)
[error record] configure NDK header file path in Visual Studio
gslb(global server load balance)技术的一点理解
mysql使用视图报错,EXPLAIN/SHOW can not be issued; lacking privileges for underlying table