当前位置:网站首页>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];
}
}边栏推荐
- MySQL winter vacation self-study 2022 12 (1)
- 8. Go implementation of string conversion integer (ATOI) and leetcode
- Is the account opening of Guoyuan securities really safe and reliable
- mysql使用视图报错,EXPLAIN/SHOW can not be issued; lacking privileges for underlying table
- 求esp32C3板子连接mssql方法
- GUI application: socket network chat room
- [complimentary ppt] kubemeet Chengdu review: make the delivery and management of cloud native applications easier!
- STM32 key light
- Thinkphp6 integrated JWT method and detailed explanation of generation, removal and destruction
- Struct in linked list
猜你喜欢
![[complimentary ppt] kubemeet Chengdu review: make the delivery and management of cloud native applications easier!](/img/28/fc05f2e8d53cf81fd061c799090022.jpg)
[complimentary ppt] kubemeet Chengdu review: make the delivery and management of cloud native applications easier!
![[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)](/img/44/aa4963d07d046deb2bc76eb59f8ff7.jpg)
[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)

Gauss elimination method and template code

Self study software testing. To what extent can you go out and find a job?

1-redis architecture design to use scenarios - four deployment and operation modes (Part 1)

STM32 key light

(Introduction to database system | Wang Shan) Chapter V database integrity: Exercises
![CesiumJS 2022^ 源码解读[8] - 资源封装与多线程](/img/d2/99932660298b4a4cddd7e5e69faca1.png)
CesiumJS 2022^ 源码解读[8] - 资源封装与多线程

Interview script of Software Test Engineer

It is worthy of "Alibaba internal software test interview notes" from beginning to end, all of which are essence
随机推荐
STM32 key light
Oracle database knowledge points (I)
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,
Beijing invites reporters and media
Pytest unit test framework: simple and easy to use parameterization and multiple operation modes
Cesiumjs 2022^ source code interpretation [8] - resource encapsulation and multithreading
Employees' turnover intention is under the control of the company. After the dispute, the monitoring system developer quietly removed the relevant services
功能:求出菲波那契数列的前一项与后一项之比的极限的 近似值。例如:当误差为0.0001时,函数值为0.618056。
Generic
Oracle database knowledge points that cannot be learned (II)
What insurance products should be bought for the elderly?
CLP information - how does the digital transformation of credit business change from star to finger?
Anomalies seen during the interview
On covariance of array and wildcard of generic type
In the process of seeking human intelligent AI, meta bet on self supervised learning
查询效率提升10倍!3种优化方案,帮你解决MySQL深分页问题
基于.NetCore开发博客项目 StarBlog - (14) 实现主题切换功能
Pair
Print diamond pattern
On the day when 28K joined Huawei testing post, I cried: everything I have done in these five months is worth it