当前位置:网站首页>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];
}
}
边栏推荐
- 中电资讯-信贷业务数字化转型如何从星空到指尖?
- Makefile judge custom variables
- 2022 Software Test Engineer skill list, please check
- Cloud dial test helps Weidong cloud education to comprehensively improve the global user experience
- [common error] custom IP instantiation error
- Cesiumjs 2022^ source code interpretation [8] - resource encapsulation and multithreading
- 手机异步发送短信验证码解决方案-Celery+redis
- Why use get/set instead of exposing properties
- 【.NET+MQTT】. Net6 environment to achieve mqtt communication, as well as bilateral message subscription and publishing code demonstration of server and client
- Long article review: entropy, free energy, symmetry and dynamics in the brain
猜你喜欢
Alibaba test engineer with an annual salary of 500000 shares notes: a complete set of written tests of software testing
[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)
[common error] UART cannot receive data error
[complimentary ppt] kubemeet Chengdu review: make the delivery and management of cloud native applications easier!
【.NET+MQTT】. Net6 environment to achieve mqtt communication, as well as bilateral message subscription and publishing code demonstration of server and client
OS interrupt mechanism and interrupt handler
What is regression testing? Talk about regression testing in the eyes of Ali Test Engineers
技術實踐|線上故障分析及解决方法(上)
MySQL winter vacation self-study 2022 12 (1)
(Introduction to database system | Wang Shan) Chapter V database integrity: Exercises
随机推荐
be based on. NETCORE development blog project starblog - (14) realize theme switching function
STM32 key light
I don't care about you. OKR or KPI, PPT is easy for you
Characteristics of ginger
Mobile asynchronous sending SMS verification code solution -efficiency+redis
Oracle database knowledge points that cannot be learned (II)
不得不会的Oracle数据库知识点(三)
[common error] UART cannot receive data error
基于.NetCore开发博客项目 StarBlog - (14) 实现主题切换功能
Future源码一观-JUC系列
2-Redis架构设计到使用场景-四种部署运行模式(下)
What is the potential of pocket network, which is favored by well-known investors?
Pytest unit test framework: simple and easy to use parameterization and multiple operation modes
What is regression testing? Talk about regression testing in the eyes of Ali Test Engineers
be based on. NETCORE development blog project starblog - (14) realize theme switching function
中电资讯-信贷业务数字化转型如何从星空到指尖?
Why use get/set instead of exposing properties
Beijing invites reporters and media
Global and Chinese markets for instant saliva testing devices 2022-2028: Research Report on technology, participants, trends, market size and share
[dynamic programming] leetcode 53: maximum subarray sum