当前位置:网站首页>Leetcode: a single element in an ordered array
Leetcode: a single element in an ordered array
2022-07-03 22:16:00 【White speed Dragon King's review】

Ideas :
Strict rules logn That's two points
How to search , Just look at the small ones index Whether the parity of is the same or not
If it's two points, make an initial judgment
then left < right, mid = (left + right) // 2, Then proceed left and right Update
src:
class Solution:
def singleNonDuplicate(self, nums: List[int]) -> int:
# Obviously, it's two points
# nums Too big to get n To do
n = len(nums)
left, right = 0, n - 1
if n == 1:
return nums[0]
if nums[left] != nums[left + 1]:
return nums[left]
if nums[right] != nums[right - 1]:
return nums[right]
def findSmallIndex(x):
if x == 0:
return 0
if x == n - 1:
return n - 2
if nums[x] == nums[x - 1]:
return x - 1
if nums[x] == nums[x + 1]:
return x
while left < right:
mid = (left + right) // 2 + 1
if nums[mid] != nums[mid - 1] and nums[mid] != nums[mid + 1]:
return nums[mid]
# Adjust the
left = findSmallIndex(left)
mid = findSmallIndex(mid)
right = findSmallIndex(right)
# left To mid There is a problem
if (mid - left) % 2 == 1:
right = mid - 1
else:
# forehead , Just add one
left = mid + 1
return -1
summary :
logn It's two points
边栏推荐
- Collection | pytoch common loss function disassembly
- Implementation principle of inheritance, encapsulation and polymorphism
- Tkinter Huarong Road 4x4 tutorial III
- [sg function] lightoj Partitioning Game
- js demo 計算本年度還剩下多少天
- Cognitive fallacy: what is Fredkin's paradox
- Cognitive fallacy: what is dimensional curse
- 2022 high altitude installation, maintenance and removal of examination question bank and high altitude installation, maintenance and removal of examination papers
- Introduction to kubernetes
- Niuke winter vacation training camp 4 g (enumeration optimization, Euler power reduction)
猜你喜欢

Preliminary analysis of smart microwave radar module

Code in keil5 -- use the code formatting tool astyle (plug-in)

3 environment construction -standalone
![[SRS] build a specified version of SRS](/img/01/0d2d762e01b304220b8924d20277e3.jpg)
[SRS] build a specified version of SRS

UC Berkeley proposes a multitask framework slip

The latest analysis of crane driver (limited to bridge crane) in 2022 and the test questions and analysis of crane driver (limited to bridge crane)

Yyds dry goods inventory Spring Festival "make" your own fireworks

Harbor integrated LDAP authentication

QGIS grid processing DEM data reclassification

Data consistency between redis and database
随机推荐
2022 G3 boiler water treatment registration examination and G3 boiler water treatment examination papers
How to obtain opensea data through opensea JS
[dynamic programming] Ji Suan Ke: Suan tou Jun breaks through the barrier (variant of the longest increasing subsequence)
regular expression
Correlation
DOM light switch case
Farmersworld farmers world, no faith, how to talk about success?
Go Technology Daily (2022-02-13) - Summary of experience in database storage selection
320. Energy Necklace (ring, interval DP)
Common SQL sets
Buuctf, web:[geek challenge 2019] buyflag
Harbor integrated LDAP authentication
Yyds dry inventory hcie security Day12: concept of supplementary package filtering and security policy
Plug - in Oil Monkey
Investment analysis and prospect trend prediction report of China's boron nitride industry Ⓨ 2022 ~ 2028
Bluebridge cup Guoxin Changtian single chip microcomputer -- hardware environment (I)
Code in keil5 -- use the code formatting tool astyle (plug-in)
Investment planning analysis and prospect prediction report of China's satellite application industry during the 14th five year plan Ⓑ 2022 ~ 2028
JS closure knowledge points essence
Supply and demand situation and market scale calculation report of China's portable energy storage power PES industry Ⓛ 2022 ~ 2028