当前位置: 首页 > news >正文

单序列双指针

class Solution:def reverseString(self, s: List[str]) -> None:# tmp = ''right = len(s) - 1left = 0while left < right:# 在python中这句与下面三行等价s[left], s[right] = s[right], s[left]# tmp = s[left]# s[left] = s[right]# s[right] = tmpleft += 1right -= 1

class Solution:def reverseSubmatrix(self, grid: List[List[int]], x: int, y: int, k: int) -> List[List[int]]:down = x + k - 1while x < down:for i in range(y, y+k):grid[x][i], grid[down][i] = grid[down][i], grid[x][i]x += 1down -= 1return grid

class Solution:def isPalindrome(self, s: str) -> bool:left = 0right = len(s) - 1while left < right:if not s[left].isalnum():left += 1elif not s[right].isalnum():right -= 1elif s[left].lower() != s[right].lower():return Falseelse:left += 1right -= 1return True
方法作用示例
isalnum()字母或数字'a1'.isalnum() → True
isalpha()仅字母(不含数字)'a1'.isalpha() → False
isdigit()仅数字(0-9)'12'.isdigit() → True
isnumeric()数字字符(包括 Unicode 数字)'Ⅷ'.isnumeric() → True

class Solution:def minimumLength(self, s: str) -> int:left = tmp = 0right = len(s) - 1while left < right and s[left] == s[right]:tmp = s[left]left += 1right -= 1while left <= right and s[left] == tmp:left += 1while left <= right and s[right] == tmp:right -= 1return right - left + 1

class Solution:def minimumRefill(self, plants: List[int], capacityA: int, capacityB: int) -> int:left = cnt = 0right = len(plants) - 1a, b = capacityA, capacityBwhile left < right:if a < plants[left]:cnt += 1a = capacityAa -= plants[left]left += 1if b < plants[right]:cnt += 1b = capacityBb -= plants[right]right -= 1if left == right and max(a, b) < plants[left]:cnt += 1return cnt

# class Solution:
#     def sortedSquares(self, nums: List[int]) -> List[int]:
#         left, right = 0, len(nums) - 1
#         while left < right:
#             nums[left] = nums[left]*nums[left]
#             nums[right] = nums[right]*nums[right]
#             left += 1
#             right -= 1
#         if left == right:
#             nums[left] = nums[left]*nums[left]
#         nums.sort()
#         return nums
class Solution:def sortedSquares(self, nums: List[int]) -> List[int]:l = len(nums)left, right = 0, l - 1ans = [0]*len(nums)for i in range(l - 1, -1, -1):a = nums[left]*nums[left]b = nums[right]*nums[right]if a > b:ans[i] = aleft += 1else:ans[i] = bright -= 1return ans

class Solution:def findClosestElements(self, arr: List[int], k: int, x: int) -> List[int]:left, right = 0, len(arr) - 1while right - left + 1 != k:if abs(arr[left] - x) <= abs(arr[right] - x):right -= 1else:left += 1return arr[left:right+1]

class Solution:def getStrongest(self, arr: List[int], k: int) -> List[int]:left, right = 0, len(arr) - 1arr.sort()mid = arr[(len(arr) - 1) // 2]nums = [0]*kfor index in range(k):if abs(arr[right] - mid) < abs(arr[left] - mid):nums[index] = arr[left]left += 1else:nums[index] = arr[right]right -= 1return nums

class Solution:def twoSum(self, numbers: List[int], target: int) -> List[int]:left, right = 0, len(numbers) - 1while True:if numbers[left] + numbers[right] > target:right -= 1elif numbers[left] + numbers[right] < target:left += 1else:return [left+1, right+1]

class Solution:def judgeSquareSum(self, c: int) -> bool:a, b = 0, isqrt(c)while a <= b:s = a*a + b*bif s == c:return Trueelif s < c:a += 1else:b -= 1return False
函数返回类型功能描述示例(输入 10)
math.sqrt浮点数(float计算平方根,可能包含小数部分(即使结果是整数)。sqrt(10) → 3.162277...
math.isqrt整数(int仅返回整数部分,向下取整(等价于 floor(sqrt(n)),但专为整数优化)。isqrt(10) → 3

class Solution:def countPairs(self, nums: List[int], target: int) -> int:nums.sort()cnt = 0left, right = 0, len(nums) - 1while left < right:if nums[left] + nums[right] < target:cnt += right - leftleft += 1else:right -= 1return cnt

class Solution:def countFairPairs(self, nums: List[int], lower: int, upper: int) -> int:nums.sort()def count(upper):left = cnt = 0right = len(nums) - 1while left < right:if nums[left] + nums[right] <= upper:cnt += right - leftleft += 1else: right -= 1return cntreturn count(upper) - count(lower - 1)

class Solution:def triangleNumber(self, nums: List[int]) -> int:if len(nums) < 3:return 0cnt = 0nums.sort()for j in range(len(nums) - 1, 1, -1):left, right = 0, j - 1while left < right:if nums[left] + nums[right] > nums[j]:cnt += right - leftright -= 1else:left += 1return cnt

class Solution:def sortArrayByParity(self, nums: List[int]) -> List[int]:left, right = 0, len(nums) - 1while left < right:if nums[left] % 2 == 0:left += 1elif nums[right] % 2 != 0:right -= 1else:nums[left], nums[right] = nums[right], nums[left]left += 1right -= 1return nums

class Solution:def moveZeroes(self, nums: List[int]) -> None:i0 = 0for i in range(len(nums)):if nums[i]:nums[i], nums[i0] = nums[i0], nums[i]i0 += 1

class Solution:def removeDuplicates(self, nums: List[int]) -> int:k = 1for i in range(1, len(nums)):if nums[i] != nums[i - 1]:nums[k] = nums[i]k += 1return k


文章转载自:

http://DCJMXzqX.nzfyx.cn
http://waw9MTVj.nzfyx.cn
http://z5utmFHP.nzfyx.cn
http://FO4E2lfu.nzfyx.cn
http://yugnSQQq.nzfyx.cn
http://BC4bO3UN.nzfyx.cn
http://IfU8M6kz.nzfyx.cn
http://uHs1vPG5.nzfyx.cn
http://LLO711dF.nzfyx.cn
http://sgRJpdw2.nzfyx.cn
http://y1Io4To2.nzfyx.cn
http://56jr5POs.nzfyx.cn
http://dsLQ0sWY.nzfyx.cn
http://mG8wt3zi.nzfyx.cn
http://QRME4gkN.nzfyx.cn
http://QRzOlkZk.nzfyx.cn
http://1bvv3AXF.nzfyx.cn
http://u6ScWTdq.nzfyx.cn
http://fJoSZ4ZP.nzfyx.cn
http://yNpfgMTh.nzfyx.cn
http://AaC4ofB0.nzfyx.cn
http://GlcpfPPT.nzfyx.cn
http://gXy7yl6S.nzfyx.cn
http://w3KOmizv.nzfyx.cn
http://msGNzVLe.nzfyx.cn
http://LAiG7ML7.nzfyx.cn
http://8k05ksea.nzfyx.cn
http://E5VKHuaa.nzfyx.cn
http://C7rp5tic.nzfyx.cn
http://hYBlE7YO.nzfyx.cn
http://www.dtcms.com/a/380744.html

相关文章:

  • Linux中进程和线程常用的API详解
  • 【AI论文】多模态大型语言模型的视觉表征对齐
  • php学习(第四天)
  • Vue中使用keep-alive实现页面前进刷新、后退缓存的完整方案
  • Jenkins运维之路(Jenkins流水线改造Day02-1-容器项目)
  • Netty从0到1系列之Netty逻辑架构【上】
  • com.google.common.cache实现本地缓存
  • LeetCode 面试经典 150 题之最后一个单词的长度:逆向遍历高效解法
  • 详解JWT
  • Spring Boot 分布式事务常见问题:Seata、XA 与本地消息表对比
  • 如何在Hugging Face中下载全部文件?
  • AI之VideoTool:AI-Video-Transcriber​​​​​​​的简介、安装和使用方法、案例应用之详细攻略
  • Qt6实现了一个打地鼠小游戏,支持AI自动打地鼠
  • Chapter5—抽象工厂模式
  • WebSocket连接状态监控与自动重连实现
  • 目标计数论文阅读(1)Class-Agnostic Counting
  • LVGL移植2048小游戏全攻略
  • 大模型系列——ChatBI重构企业知识库
  • DEM(数字高程模型)详解
  • 软考 系统架构设计师系列知识点之杂项集萃(144)
  • R语言生物群落(生态)数据统计分析与绘图实践技术应用
  • DPO 深度解析:从公式到工程,从偏好数据到可复用训练管线
  • 今天继续学习Linux系统中shell脚本
  • 开源端到端训练多模态大模型LLaVA 深度拆解
  • 周志华《机器学习导论》第10章 降维与度量学习
  • PyQt置顶窗口
  • 基于图像和激光的多模态点云融合与视觉定位
  • 企业数据防护利器:Curtain e-locker 支持NCA合规
  • 【Vue2 ✨】Vue2 入门之旅 · 进阶篇(九):Vue2 性能优化
  • Java面试问题记录(二)