当前位置:网站首页>leetcode 数组类
leetcode 数组类
2022-07-26 19:58:00 【Yuki_1999】
704题:二分查找
给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target ,写一个函数搜索 nums 中的 target,如果目标值存在返回下标,否则返回 -1。
二分查找
1)条件:元素有序,无重复元素
2)过程:假设表中元素升序排列,中间位置元素与查找元素比较,相等,则查找成功;否则利用中间位置记录将表分成前、后两个子表,如果中间位置记录的关键字大于查找关键字,则进一步查找前一子表,否则进一步查找后一子表。
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题:移除元素
给你一个数组 nums 和一个值 val,你需要 原地 移除所有数值等于 val 的元素,并返回移除后数组的新长度。
双指针法(快慢指针法)
1)定义:快指针指向当前要处理的元素,慢指针指向下一个要赋值的位置
2)使用场合:很多考察数组、链表、字符串等操作的面试题,都使用双指针法。
class Solution(object):
def removeElement(self, nums, val):
left=0
for right in range(0,len(nums)):
#不相等则在原数组上覆盖
if nums[right]!=val:
nums[left]=nums[right]
left+=1
return left
边栏推荐
- NVIDIA Canvas 初体验~
- Keepalived high availability introduction and configuration details
- 分组卷积(Group Converlution)
- Sword finger offer46 translates numbers into strings
- Shell综合应用案例,归档文件
- Leetcode-300 longest increasing subsequence
- [基础服务] [数据库] ClickHouse的安装和配置
- Principle and application of one click login of local number (glory Collection Edition)
- Read the four service types of kubernetes!
- Auto. JS rotation Icon
猜你喜欢

Establishment of APP automation testing framework (VIII) -- establishment of ATX server2 multi device cluster environment

Message queue -- the problem introduced: repeated consumption & sequential consumption & distributed transactions

QT driving school subject examination system -- from implementation to release

BGP--边界网关协议

【微信小程序】零基础学 | 小程序语法

Experiment 6 BGP federal comprehensive experiment
![[基础服务] [数据库] ClickHouse的安装和配置](/img/fe/5c24e4c3dc17a6a96985e4fe97024e.png)
[基础服务] [数据库] ClickHouse的安装和配置

分组卷积(Group Converlution)

The sandbox cooperates with artist Alec monopoly

Marketing and sales document management and workflow solutions
随机推荐
81. (cesium home) cesium modifies the gray background (default blue)
How to upload and download files
Buu brush inscription 3
20220726
YGG cooperates with my pet hooligan, AMGI's flagship NFT project, to enter the rabbit hole
Buu brush inscription 2
It is said that HP / Dell / Microsoft / Amazon are considering transferring some hardware production lines outside the mainland
Auto. JS rotation Icon
opencv dnn部署onnx模型
APP自动化测试框架搭建(八)--ATX Server2多设备集群环境搭建
Bean注入和生命周期
Summary of message queue knowledge points
QT signal and slot connection (loose coupling)
Chat software project development 2
Confid+etcd to realize automatic discovery of high availability
How to praise others gracefully? Try these methods
「企业管理」精诚CRM+——一体化管理企业业务流程
深度可分离卷积(DepthwiseSeparableConvolution):Depthwise卷积与Pointwise卷积
Leetcode-300 longest increasing subsequence
Applied materials announced that it would acquire international electric with us $2.2 billion in cash