当前位置:网站首页>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
边栏推荐
- [actual combat record] record the whole process of the server being attacked (redis vulnerability)
- 常用sql集合
- Oil monkey plug-in
- How PHP drives mongodb
- Bluebridge cup Guoxin Changtian single chip microcomputer -- detailed explanation of schematic diagram (IV)
- 十大券商开户注册安全靠谱吗?有没有风险的?
- How to install sentinel console
- Codeforces Round #768 (Div. 1)(A-C)
- China's TPMS industry demand forecast and future development trend analysis report Ⓐ 2022 ~ 2028
- Persistence of Nacos
猜你喜欢
pivot ROP Emporium
Exness: the Central Bank of England will raise interest rates again in March, and inflation is coming
string
Persistence of Nacos
On my first day at work, this API timeout optimization put me down!
[flax high frequency question] leetcode 426 Convert binary search tree to sorted double linked list
3 environment construction -standalone
Mysql database - Advanced SQL statement (I)
Intimacy communication -- [repair relationship] - use communication to heal injuries
Exclusive interview with the person in charge of openkruise: to what extent has cloud native application automation developed now?
随机推荐
Cognitive fallacy: what is Fredkin's paradox
油猴插件
IPhone development swift foundation 08 encryption and security
Mindmanager2022 serial number key decompression installer tutorial
Rest参考
China HDI market production and marketing demand and investment forecast analysis report Ⓢ 2022 ~ 2028
使用dnSpy對無源碼EXE或DLL進行反編譯並且修改
The latest analysis of R1 quick opening pressure vessel operation in 2022 and the examination question bank of R1 quick opening pressure vessel operation
2022 electrician (elementary) examination questions and electrician (elementary) registration examination
Team collaborative combat penetration tool CS artifact cobalt strike
What is the difference between res.send() and res.end() in the node express framework
Preliminary analysis of smart microwave radar module
Analysis report on the development trend and Prospect of global and Chinese supercontinuum laser source industry Ⓚ 2022 ~ 2027
Uboot migration
Correlation
Buuctf, web:[geek challenge 2019] buyflag
Leetcode problem solving - 230 The k-th smallest element in the binary search tree
Codeforces Round #768 (Div. 1)(A-C)
regular expression
How to obtain opensea data through opensea JS