当前位置:网站首页>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
边栏推荐
- Rest reference
- How does sentinel, a traffic management artifact, make it easy for business parties to access?
- WiFi 2.4g/5g/6g channel distribution
- Yyds dry inventory Chapter 4 of getting started with MySQL: data types that can be stored in the data table
- Supply and demand situation and market scale calculation report of China's portable energy storage power PES industry Ⓛ 2022 ~ 2028
- Collection | pytoch common loss function disassembly
- DR882-Qualcomm-Atheros-QCA9882-2T2R-MIMO-802.11ac-Mini-PCIe-Wi-Fi-Module-5G-high-power
- English topic assignment (28)
- Go language slice interview real question 7 consecutive questions
- Bluebridge cup Guoxin Changtian single chip microcomputer -- detailed explanation of schematic diagram (IV)
猜你喜欢
Dahua series books
2 spark environment setup local
Control loop of program (while loop)
Pooling idea: string constant pool, thread pool, database connection pool
Farmersworld farmers world, no faith, how to talk about success?
How PHP drives mongodb
[dynamic programming] Ji Suan Ke: Suan tou Jun breaks through the barrier (variant of the longest increasing subsequence)
Buuctf, misc: sniffed traffic
[flax high frequency question] leetcode 426 Convert binary search tree to sorted double linked list
STM32 multi serial port implementation of printf -- Based on cubemx
随机推荐
Awk getting started to proficient series - awk quick start
What is the difference between res.send() and res.end() in the node express framework
Blue Bridge Cup Guoxin Changtian single chip microcomputer -- software environment (II)
This time, thoroughly understand bidirectional data binding 01
SDNU_ ACM_ ICPC_ 2022_ Winter_ Practice_ 4th [individual]
2022 G3 boiler water treatment registration examination and G3 boiler water treatment examination papers
1 Introduction to spark Foundation
DOM light switch case
[sg function]split game (2020 Jiangxi university student programming competition)
Leetcode problem solving - 230 The k-th smallest element in the binary search tree
2 spark environment setup local
The 14th five year plan for the construction of Chinese Enterprise Universities and the feasibility study report on investment Ⓓ 2022 ~ 2028
An expression that regularly matches one of two strings
Report on the development strategy of China's engineering bidding agency and suggestions for the 14th five year plan Ⓙ 2022 ~ 2028
Sed、Awk
Luogu deep foundation part 1 Introduction to language Chapter 7 functions and structures
Teach you how to install aidlux (1 installation)
[Android reverse] application data directory (files data directory | lib application built-in so dynamic library directory | databases SQLite3 database directory | cache directory)
[sg function] 2021 Niuke winter vacation training camp 6 h. winter messenger 2
Collection | pytoch common loss function disassembly