当前位置:网站首页>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]边栏推荐
- 谈SaaS下如何迅速部署应用软件
- Unity script API - component component
- 夜天之书 #53 Apache 开源社群的“石头汤”
- LNX efficient search engine, fastdeploy reasoning deployment toolbox, AI frontier paper | showmeai information daily # 07.04
- 文本挖掘工具的介绍[通俗易懂]
- MySQL federated primary key_ MySQL creates a federated primary key [easy to understand]
- In today's highly integrated chips, most of them are CMOS devices
- %F format character
- The new generation of domestic ORM framework sagacity sqltoy-5.1.25 release
- QT graphical view frame: element movement
猜你喜欢
MySQL组合索引(多列索引)使用与优化案例详解

Quelles sont les perspectives de l'Internet intelligent des objets (aiot) qui a explosé ces dernières années?
Redis sentinel mode realizes one master, two slave and three Sentinels

Nine CIO trends and priorities in 2022
![[North Asia data recovery] data recovery case of database data loss caused by HP DL380 server RAID disk failure](/img/f0/12dd17e840a23dc9ded379e1fd7454.jpg)
[North Asia data recovery] data recovery case of database data loss caused by HP DL380 server RAID disk failure

中国主要城市人均存款出炉,你达标了吗?

在芯片高度集成的今天,绝大多数都是CMOS器件

干货 | fMRI标准报告指南新鲜出炉啦,快来涨知识吧

Audio and video technology development weekly | 252

Live broadcast preview | PostgreSQL kernel Interpretation Series II: PostgreSQL architecture
随机推荐
Go zero micro service practical series (IX. ultimate optimization of seckill performance)
Redis sentinel mode realizes one master, two slave and three Sentinels
函数式接口,方法引用,Lambda实现的List集合排序小工具
LeetCode 1184. 公交站间的距离 ---vector顺逆时针
.Net之延迟队列
暑期复习,一定要避免踩这些坑!
MySQL学习笔记——数据类型(数值类型)
After the eruption of Tonga volcano, we analyzed the global volcanic distribution and found that the area with the most volcanoes is here!
[book club issue 13] ffmpeg common methods for viewing media information and processing audio and video files
Nine CIO trends and priorities in 2022
2022年九大CIO趨勢和優先事項
Common API day03 of unity script
The 17 year growth route of Zhang Liang, an open source person, can only be adhered to if he loves it
. Net delay queue
MySQL learning notes - data type (numeric type)
Width and alignment
%S format character
Summer Review, we must avoid stepping on these holes!
MySQL index optimization
In today's highly integrated chips, most of them are CMOS devices