目录
283. 移动零
Python
class Solution:
def moveZeroes(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
n=len(nums)
i=0
for j in range(n):
if nums[j]!=0:
nums[i],nums[j]=nums[j],nums[i]
i+=1 # i指向0,因为当前数字不为0才前进
153. 寻找旋转排序数组中的最小值
Python
class Solution:
def findMin(self, nums: List[int]) -> int:
low,high=0,len(nums)-1
while low<high:
mid=(low+high)//2
# 元素值互不相同
if nums[mid]<nums[high]:
high=mid
else: # nums[mid]>nums[high]:
low=mid+1
return nums[low]
类似题目 33. 搜索旋转排序数组
468. 验证IP地址
Python
class Solution:
def validIPAddress(self, queryIP: str) -> str:
def validIPv6(IP_string):
IP_lst = IP_string.split(':')
if len(IP_lst) != 8: # IPv6必须由8个子串组成
return False
for IP in IP_lst:
if not 1 <= len(IP) <= 4: # 每个子串必须小于4个字符
return False
for char in IP:
if not ('0' <= char <= '9' or 'a' <= char <= 'f' or 'A' <= char