当前位置:网站首页>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
边栏推荐
- pivot ROP Emporium
- What indicators should be paid attention to in current limit monitoring?
- LeetCode 1646. Get the maximum value in the generated array
- LeetCode 1647. Minimum deletion times of unique character frequency
- Persistence of Nacos
- Pooling idea: string constant pool, thread pool, database connection pool
- 2022 safety officer-b certificate examination summary and safety officer-b certificate simulation test questions
- Development trend and market demand analysis report of China's energy storage battery industry Ⓩ 2022 ~ 2028
- Market layout planning and latest dynamic analysis report of China's smart public security industry Ⓕ 2022 ~ 2028
- Data consistency between redis and database
猜你喜欢

1 Introduction to spark Foundation

Decompile and modify the non source exe or DLL with dnspy

Redis single thread and multi thread

Kali2021.4a build PWN environment

STM32 multi serial port implementation of printf -- Based on cubemx

Station B, dark horse programmer, employee management system, access conflict related (there is an unhandled exception at 0x00007ff633a4c54d (in employee management system.Exe): 0xc0000005: read locat

1068. Consolidation of ring stones (ring, interval DP)
![Intimacy communication -- [repair relationship] - use communication to heal injuries](/img/c2/f10405e3caf570dc6bd124d65b2e93.jpg)
Intimacy communication -- [repair relationship] - use communication to heal injuries

Common SQL sets

The latest analysis of R1 quick opening pressure vessel operation in 2022 and the examination question bank of R1 quick opening pressure vessel operation
随机推荐
Is it safe and reliable to open an account and register for stock speculation? Is there any risk?
Yyds dry goods inventory hands-on teach you to create a jigsaw puzzle using the canvasapi
WFC900M-Network_ Card/Qualcomm-Atheros-AR9582-2T-2R-MIMO-802.11-N-900M-high-power-Mini-PCIe-Wi-Fi-Mod
Data consistency between redis and database
China's coal industry investment strategic planning future production and marketing demand forecast report Ⓘ 2022 ~ 2028
320. Energy Necklace (ring, interval DP)
Codeforces Round #768 (Div. 1)(A-C)
Pooling idea: string constant pool, thread pool, database connection pool
6.0 kernel driver character driver
Cesium terrain clipping draw polygon clipping
股票炒股开户注册安全靠谱吗?有没有风险的?
DR-NAS26-Qualcomm-Atheros-AR9582-2T-2R-MIMO-802.11-N-5GHz-high-power-Mini-PCIe-Wi-Fi-Module
Leetcode problem solving - 235 Nearest common ancestor of binary search tree
Electronic tube: Literature Research on basic characteristics of 6j1
WiFi 2.4g/5g/6g channel distribution
Rest reference
Control loop of program (while loop)
Yyds dry inventory hcie security Day12: concept of supplementary package filtering and security policy
AST (Abstract Syntax Tree)
How PHP adds two numbers