当前位置:网站首页>One question per day 540 A single element in an ordered array
One question per day 540 A single element in an ordered array
2022-07-04 16:01:00 【A big pigeon】
topic : 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
Explain : See the title in O(log n) Time complexity , It's easy to think of binary search . The difficulty is that this problem is not to find a certain value , Instead, look for a single number . Conditions nums[mid] ?? target How to change ?
Individual elements are preceded by n The element , namely 2n Elements , So the subscripts of individual elements must be even . Binary search for even subscripts .
If it is related to num[mid+1] It's a pair. , It means that the front ones are all in pairs ,low= mid+2; Otherwise, there is a separate ,high = mid.
class Solution:
def singleNonDuplicate(self, nums: List[int]) -> int:
low, high = 0, len(nums) - 1
while low < high:
mid = (low + high) // 2
mid -= mid & 1
#mid -= mid & 1 The function is to make mid Take even number .& It's bitwise and , Odd number &1 = 1, even numbers &1 =0.
if nums[mid] == nums[mid + 1]:
low = mid + 2
else:
high = mid
return nums[low]
If while Recycle <= Conditions , need high Initialize to len(nums)-2, Otherwise, the array will be out of bounds . The search space is [0,len(nums)-2], actually mid Maximum value len(nums)-2 In the interval , So there will be no omission .
class Solution:
def singleNonDuplicate(self, nums: List[int]) -> int:
low, high = 0, len(nums) - 2
while low <= high:
mid = (low + high) // 2
mid -= mid & 1
if nums[mid] == nums[mid + 1]:
low = mid + 2
else:
high = mid-2
return nums[low]边栏推荐
- Hexadecimal form
- [hcie TAC] question 5 - 1
- Stress, anxiety or depression? Correct diagnosis and retreatment
- Unity script API - GameObject game object, object object
- unity update 协程_Unity 协程的原理
- Redis sentinel mode realizes one master, two slave and three Sentinels
- 数据库函数的用法「建议收藏」
- [book club issue 13] packaging format and coding format of audio files
- MySQL federated primary key_ MySQL creates a federated primary key [easy to understand]
- Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example
猜你喜欢
![[Dalian University of technology] information sharing of postgraduate entrance examination and re examination](/img/06/df5a64441814c9ecfa2f039318496e.jpg)
[Dalian University of technology] information sharing of postgraduate entrance examination and re examination

Dry goods | fMRI standard reporting guidelines are fresh, come and increase your knowledge

开源人张亮的 17 年成长路线,热爱才能坚持

LNX efficient search engine, fastdeploy reasoning deployment toolbox, AI frontier paper | showmeai information daily # 07.04

大神详解开源 BUFF 增益攻略丨直播

error: ‘connect‘ was not declared in this scope connect(timer, SIGNAL(timeout()), this, SLOT(up

案例分享|金融业数据运营运维一体化建设

压力、焦虑还是抑郁? 正确诊断再治疗

Book of night sky 53 "stone soup" of Apache open source community

C1 certification learning notes 3 -- Web Foundation
随机推荐
What encryption algorithm is used for the master password of odoo database?
Neuf tendances et priorités du DPI en 2022
Data Lake Governance: advantages, challenges and entry
【读书会第十三期】FFmpeg 查看媒体信息和处理音视频文件的常用方法
Common API day03 of unity script
Unity script API - GameObject game object, object object
hexadecimal
Selenium browser (2)
Rearrange array
MYSQL索引优化
QT graphical view frame: element movement
Shell programming basics
Unity脚本API—Transform 变换
[book club issue 13] coding format of video files
Interface test - knowledge points and common interview questions
[hcie TAC] question 5 - 1
CentOS 6.3 下 PHP编译安装JSON模块报错解决
PR FAQ: how to set PR vertical screen sequence?
[tutorial] yolov5_ DeepSort_ The whole process of pytoch target tracking and detection
How to save the contents of div as an image- How to save the contents of a div as a image?