
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