python中nums.count_leetcode数组类算法(Python3)学习笔记,LeetCode

概述

做好初始定义

移动零

\\我的解法

for i in range(nums.count(0)):

nums.remove(0)

nums.append(0)

\\执行用时:112 ms, 在所有 Python3 提交中击败了17.37% 的用户

\\内存消耗:14.1 MB, 在所有 Python3 提交中击败了30.51% 的用户

\\ 官方解法

n = len(nums)

i = -1

j = 0

# nums[0....i]表示非0元素的数列,初始值i=-1

while j <= n-1:

if nums[j] != 0:

i += 1

nums[i] = nums[j]

j += 1

for k in range(i+1, n):

nums[k] = 0

\\ 执行用时:44 ms, 在所有 Python3 提交中击败了68.20% 的用户

\\内存消耗:14.3 MB, 在所有 Python3 提交中击败了5.30% 的用户

移除元素

\\我的解法 比官方用时短

i = -1

j = 0

n = len(nums)

while j < n:

if nums[j] != val:

j+=1

else:

del nums[j]

n-=1

print(n)

return n

\\执行用时:36 ms, 在所有 Python3 提交中击败了86.99% 的用户

\\内存消耗:13.6 MB, 在所有 Python3 提交中击败了5.15% 的用户

\\官方解法

删除排序数组中的重复项 I

\\我的解法

i=0

n=len(nums)

for j in range(1,n):

if nums[i]!=nums[j]:

i+=1

nums[i]=nums[j]

print(i)

return i+1

\\执行用时:52 ms, 在所有 Python3 提交中击败了56.23% 的用户

\\内存消耗:14.8 MB, 在所有 Python3 提交中击败了5.06% 的用户

删除排序数组中的重复项 II

\\我的解法

i=1

j=0

temp=0

n=len(nums)

while i < n:

if nums[i]==nums[i-1]:

j+=1

else:

j=0

if j >= 2:

temp+=1

nums[i-temp] = nums[i]

i+=1

print(n-temp)

return n-temp

\\执行用时:68 ms, 在所有 Python3 提交中击败了6.76% 的用户

\\内存消耗:13.6 MB, 在所有 Python3 提交中击败了5.05% 的用户

运用基础算法思想

颜色分类

\\我的解法

a=nums.count(0)

b=nums.count(1)

c=nums.count(2)

for i in range(a):

nums[i]=0

for j in range(a,a+b):

nums[j]=1

for q in range(a+b,a+b+c):

nums[q]=2

\\执行用时:32 ms, 在所有 Python3 提交中击败了96.42% 的用户

\\内存消耗:13.5 MB, 在所有 Python3 提交中击败了5.64% 的用户

数组中第K个最大元素

\\我的解法

nums.sort(reverse = True)

return nums[k-1]

\\执行用时:36 ms, 在所有 Python3 提交中击败了97.86% 的用户

\\内存消耗:14.1 MB, 在所有 Python3 提交中击败了33.10% 的用户

合并两个有序数组

\\我的解法

for i in range(n):

nums1[m+i]=nums2[i]

nums1.sort()

return nums1

\\执行用时:44 ms, 在所有 Python3 提交中击败了46.63% 的用户

\\内存消耗:13.6 MB, 在所有 Python3 提交中击败了5.08% 的用户

输入有序数组-两数之和II

\\我的解法 双指针法 从左右索引开始

left , right = 0 , len(numbers)-1

while left <= right:

s = numbers[left] + numbers[right]

if s == target:

return [left+1,right+1]

elif s < target:

left += 1

else:

right -= 1

\\执行用时:32 ms, 在所有 Python3 提交中击败了99.36% 的用户

\\内存消耗:13.9 MB, 在所有 Python3 提交中击败了15.05% 的用户

验证回文串

\\我的解法 双指针 只保留数字和字母

s = s.lower()

left, right = 0, len(s) - 1

while left < right:

while left < right and not s[left].isalnum():

left += 1

while left < right and not s[right].isalnum():

right -= 1

if left < right and s[left] != s[right]:

return False

left += 1

right -= 1

return True

\\执行用时:52 ms, 在所有 Python3 提交中击败了86.48% 的用户

\\内存消耗:13.8 MB, 在所有 Python3 提交中击败了51.99% 的用户

反转字符串中的元音字母

\\我的解法 对撞指针

left,right=0,len(s)-1

array = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']

sList = list(s)

while left < right:

if sList[left] in array and sList[right] in array:

sList[left], sList[right] = sList[right], sList[left]

left += 1

right -= 1

if sList[right] not in array:

right -= 1

if sList[left] not in array:

left += 1

return ''.join(sList)

\\执行用时:64 ms, 在所有 Python3 提交中击败了74.24% 的用户

\\内存消耗:14.5 MB, 在所有 Python3 提交中击败了28.05% 的用户

盛最多水的容器

\\我的解法 :长度不断缩短,缩短的过程中只缩短值小的一边

left,right= 0, len(height)-1

AreaS=0

while left

AreaS= max(AreaS,min(height[left],height[right])*(right-left))

if height[left]

left+=1

else:

right-=1

return AreaS

\\执行用时:80 ms, 在所有 Python3 提交中击败了39.45% 的用户

\\内存消耗:14.9 MB, 在所有 Python3 提交中击败了18.77% 的用户

长度最小的子数组

\\ 我的解法

left = 0

sum1 = 0

count = float("inf")

n=len(nums)

for right in range(n):

sum1=sum1+nums[right]

while sum1 >=s:

count=min(count,right-left+1)

sum1-=nums[left]

left+=1

return count if count!=float('inf') else 0

\\执行用时:48 ms, 在所有 Python3 提交中击败了90.80% 的用户

\\内存消耗:14.9 MB, 在所有 Python3 提交中击败了81.69% 的用户

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值