当前位置:网站首页>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]
边栏推荐
- Go deep into the details of deconstruction and assignment of several data types in JS
- [tutorial] yolov5_ DeepSort_ The whole process of pytoch target tracking and detection
- 大神详解开源 BUFF 增益攻略丨直播
- The four most common errors when using pytorch
- Can I "reverse" a Boolean value- Can I 'invert' a bool?
- Implementation of web chat room
- Interpretation of the champion scheme of CVPR 2020 night target detection challenge
- Unity脚本生命周期 Day02
- Nine CIO trends and priorities in 2022
- Ten clothing stores have nine losses. A little change will make you buy every day
猜你喜欢
[North Asia data recovery] data recovery case of database data loss caused by HP DL380 server RAID disk failure
MySQL learning notes - data type (2)
Detailed explanation of MySQL composite index (multi column index) use and optimization cases
The per capita savings of major cities in China have been released. Have you reached the standard?
Dry goods | fMRI standard reporting guidelines are fresh, come and increase your knowledge
Audio and video technology development weekly | 252
每周招聘|高级DBA年薪49+,机会越多,成功越近!
What is the future of the booming intelligent Internet of things (aiot) in recent years?
How did the beyond concert 31 years ago get super clean and repaired?
The 17 year growth route of Zhang Liang, an open source person, can only be adhered to if he loves it
随机推荐
夜天之书 #53 Apache 开源社群的“石头汤”
暑期复习,一定要避免踩这些坑!
Blood cases caused by Lombok use
Live broadcast preview | PostgreSQL kernel Interpretation Series II: PostgreSQL architecture
LeetCode 1184. Distance between bus stops -- vector clockwise and counterclockwise
谈SaaS下如何迅速部署应用软件
Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example
【读书会第十三期】 音频文件的封装格式和编码格式
LeetCode 35. 搜索插入位置 —vector遍历(O(logn)和O(n)的写法---二分查找法)
数据湖治理:优势、挑战和入门
Stress, anxiety or depression? Correct diagnosis and retreatment
. Net delay queue
Qt---error: ‘QObject‘ is an ambiguous base of ‘MyView‘
Lombok使用引发的血案
Salient map drawing based on OpenCV
Essential basic knowledge of digital image processing
Selenium browser (2)
Unity脚本介绍 Day01
这几年爆火的智能物联网(AIoT),到底前景如何?
Unity脚本常用API Day03