当前位置:网站首页>Leetcode array class
Leetcode array class
2022-07-26 20:54:00 【Yuki_ one thousand nine hundred and ninety-nine】
704 topic : Two points search
Given a n The elements are ordered ( Ascending ) integer array nums And a target value target , Write a function search nums Medium target, If the target value has a return subscript , Otherwise return to -1.
Two points search
1) Conditions : The elements are in order , No repeating elements
2) The process : Suppose the elements in the table are arranged in ascending order , Compare the middle position element with the lookup element , equal , Then the search is successful ; Otherwise, use the middle position record to divide the table into front 、 Last two sub tables , If the key of the intermediate location record is greater than the search key , Then look up the previous sub table , Otherwise, look up the next sub table .
class Solution(object):
def search(self, nums, target):
left,right=0,len(nums)-1
while left<=right:
middle=(right-left)//2
if target>nums[middle]:
left=middle+1
elif target<nums[middle]:
right=middle-1
else:
return middle
return -1
27 topic : Remove elements
Give you an array nums And a value val, You need In situ Remove all values equal to val The elements of , And return the new length of the array after removal .
Double finger needling ( Fast and slow pointer method )
1) Definition : The fast pointer points to the element currently being processed , The slow pointer points to the next position to be assigned
2) Use occasion : A lot of research 、 Linked list 、 Interview questions for string and other operations , They all use double finger acupuncture .
class Solution(object):
def removeElement(self, nums, val):
left=0
for right in range(0,len(nums)):
# If they are not equal, they will be overwritten on the original array
if nums[right]!=val:
nums[left]=nums[right]
left+=1
return left
边栏推荐
- hello 你好吗
- How to check whether the pytorch you are using is a GPU version
- How to obtain Cu block partition information in HM and draw with MATLAB
- Use Baidu PaddlePaddle easydl to complete garbage classification
- Buu brush inscription 3
- Keepalived高可用介绍与配置详解
- Transaction rollback and record exception information at the same time
- Basic configuration and aggregation of BGP
- BGP的路由黑洞和防环
- Single core A7 plays with face recognition, and NXP "crossover processor" plays a new trick!
猜你喜欢

Chat software project development 2

BGP的基本配置和聚合

CentOS7关于Oracle RAC 11GR2部署磁盘分区问题

营销与销售文件管理以及工作流程解决方案

消息队列——引入的问题:重复消费&顺序消费&分布式事务
![Shell function, system function, basename [string / pathname] [suffix] can be understood as taking the file name in the path, dirname file absolute path, and user-defined function](/img/3d/d7276d2010f1d77a3bd572cc66eced.png)
Shell function, system function, basename [string / pathname] [suffix] can be understood as taking the file name in the path, dirname file absolute path, and user-defined function

Marketing and sales document management and workflow solutions

【面试必刷101】动态规划1

易基因|宏病毒组测序技术介绍

BGP的路由黑洞和防环
随机推荐
Group convolution
NVIDIA Canvas 初体验~
7.25模拟赛总结
Quick start to connection pooling
The 50 Smartest Companies in the world announced that Chinese manufacturers account for nearly half, and Huawei ranks first
Leetcode-300 最长递增子序列
MPLS multi protocol label switching technology
Pspice simulation quartz crystal oscillation circuit
详细图解b树及C语言实现
Buu brush inscription 1
从零开始搭建Prometheus自动监控报警系统
leetcode 链表类
Kotlin - coroutinecontext
St table, weighted and search set
LCP 11. 期望个数统计
Green and sustainable development of data center
Message queue -- the problem introduced: repeated consumption & sequential consumption & distributed transactions
Solve the horizontal segmentation of iBGP and BGP routing principles
LeetCode_ Backtracking_ Medium_ 216. Combined sum III
Houdini 求中点,点连成线