当前位置:网站首页>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
边栏推荐
- 单核A7玩转人脸识别,恩智浦“跨界处理器”又玩出新花样!
- Pspice simulation quartz crystal oscillation circuit
- LeetCode链表问题——24.两两交换链表中的结点(一题一文学会链表)
- 【微信小程序】零基础学 | 小程序语法
- AI technology, simplifying the complex world | teatalk online application practical series, issue 2
- The lawyer team of the US Department of justice asked the judge to refuse to accept Huawei's lawsuit
- Execution context and Lexical Environment
- 培训软件测试能不能就业
- LeetCode_ Backtracking_ Medium_ 216. Combined sum III
- The UK and Germany have successively launched 5g commercial services, and Huawei has become a behind the scenes hero
猜你喜欢
随机推荐
执行上下文与词法环境
Hello, how are you
twenty million two hundred and twenty thousand seven hundred and twenty-six
Common operations of ndarray in numpy
7-year-old boy playing chess too fast? The robot actually broke its finger
Depthwiseseparableconvolution: depthwise convolution and pointwise convolution
serializable接口的作用是什么?
How to obtain Cu block partition information in HM and draw with MATLAB
剑指offer46把数字翻译成字符串
leetcode 数组类
Why can ThreadLocal achieve thread isolation?
QT driving school subject examination system -- from implementation to release
Pspice simulation quartz crystal oscillation circuit
Software testing - development test content specification (project test template)
Single core A7 plays with face recognition, and NXP "crossover processor" plays a new trick!
Discussion on loan agreement mode with NFT as collateral
深度可分离卷积(DepthwiseSeparableConvolution):Depthwise卷积与Pointwise卷积
Leetcode-300 最长递增子序列
20220726
The UK and Germany have successively launched 5g commercial services, and Huawei has become a behind the scenes hero
![[基础服务] [数据库] ClickHouse的安装和配置](/img/fe/5c24e4c3dc17a6a96985e4fe97024e.png)







