当前位置:网站首页>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
边栏推荐
- 【问题篇】将集合[‘‘,‘‘]处理成(‘‘,‘‘)
- Discussion on loan agreement mode with NFT as collateral
- St table, weighted and search set
- Keepalived高可用介绍与配置详解
- twenty million two hundred and twenty thousand seven hundred and twenty-six
- Interceptors
- Use Baidu PaddlePaddle easydl to complete garbage classification
- 美司法部律师团队要求法官拒绝受理华为诉讼
- Quick start to connection pooling
- Bean注入和生命周期
猜你喜欢

SSM整合实例

QT基础第一天 (1)QT,GUI(图形用户接口)开发

Leetcode-300 longest increasing subsequence

Quick start to connection pooling

Houdini finds the midpoint and connects the points to form a line

连接池快速入门

Leetcode-300 最长递增子序列

Do employees have to compensate the company for losses when they resign? The 34 year old captain resigned and was claimed 10.66 million yuan by the company

Buu brush inscription 2

解决IBGP的水平分割和BGP选路原则
随机推荐
Installation and configuration of [basic services] [database] Clickhouse
英国德国相继推出5G商用服务,华为成幕后功臣
Buu brush inscription 3
Kotlin - coroutinecontext
"Enterprise management" sincere crm+ - integrated management of enterprise business processes
全球最聪明50家公司公布:中国厂商占据近半,华为名列第一
Face recognition and fingerprint recognition are weak? Pentagon develops long-distance heartbeat recognition
【面试必刷101】动态规划1
QT driving school subject examination system -- from implementation to release
培训软件测试能不能就业
QT signal and slot connection (loose coupling)
【Delphi】FMX Form的BorderStyles不同平台说明
884. 两句话中的不常见单词-哈希表
Establishment of APP automation testing framework (VIII) -- establishment of ATX server2 multi device cluster environment
【PyQt5基本控件使用解析】
LCP 11. 期望个数统计
Gartner发布最新《中国AI初创企业市场指南》,弘玑Cyclone再次被评为代表性企业
【微信小程序】零基础学 | 小程序语法
884. Uncommon words in two sentences - hash table
BTC和ETH不确定性增强 因加息逼近?美国经济将面临更多痛苦