当前位置:网站首页>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];
}
}边栏推荐
- 功能:将主函数中输入的字符串反序存放。例如:输入字符串“abcdefg”,则应输出“gfedcba”。
- MySQL winter vacation self-study 2022 12 (2)
- AI helps make new breakthroughs in art design plagiarism retrieval! Professor Liu Fang's team paper was employed by ACM mm, a multimedia top-level conference
- Global and Chinese market of breast cancer imaging 2022-2028: Research Report on technology, participants, trends, market size and share
- [common error] custom IP instantiation error
- CesiumJS 2022^ 源码解读[8] - 资源封装与多线程
- OS interrupt mechanism and interrupt handler
- Arc 135 supplementary report
- Thinkphp6 integrated JWT method and detailed explanation of generation, removal and destruction
- Technical practice online fault analysis and solutions (Part 1)
猜你喜欢

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

机器学习基础:用 Lasso 做特征选择
![[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)

MPLS experiment

How to be a professional software testing engineer? Listen to the byte five year old test

Network layer - routing

Function: store the strings entered in the main function in reverse order. For example, if you input the string "ABCDEFG", you should output "gfedcba".

The difference between objects and objects

AI helps make new breakthroughs in art design plagiarism retrieval! Professor Liu Fang's team paper was employed by ACM mm, a multimedia top-level conference

【.NET+MQTT】.NET6 环境下实现MQTT通信,以及服务端、客户端的双边消息订阅与发布的代码演示
随机推荐
Global and Chinese market of underwater bags 2022-2028: Research Report on technology, participants, trends, market size and share
A malware detection method for checking PLC system using satisfiability modulus theoretical model
Software testers, how can you quickly improve your testing skills? Ten minutes to teach you
How to use AHAS to ensure the stability of Web services?
Flutter local database sqflite
The first training of wechat applet
数据库表外键的设计
The difference between objects and objects
Is it really possible that the monthly salary is 3K and the monthly salary is 15K?
On covariance of array and wildcard of generic type
[error record] configure NDK header file path in Visual Studio
[prefix and notes] prefix and introduction and use
From functional testing to automated testing, how did I successfully transform my salary to 15K +?
Why use get/set instead of exposing properties
Network layer - routing
技術實踐|線上故障分析及解决方法(上)
国元证券开户是真的安全可靠吗
Leetcode 121 best time to buy and sell stock (simple)
Data mining vs Machine Learning: what is the difference between them? Which is more suitable for you to learn
Beijing invites reporters and media