一周做了以下题,第一次参加全球比赛的结果
文章目录
-
- .各位平方求和
- .列表转字符串
- .反转数字
- .列表出现位置
- .跳出多层循环
- .二分查找
- .移除元素
- .滑动窗口
-
- .动态规划
-
- 53. 最大子数组和
- 300. 最长递增子序列
- 674. 最长连续递增序列
- 70. 爬楼梯
- 746. 使用最小花费爬楼梯
- 416. 分割等和子集(背包)
- 198. 打家劫舍
- .哈希(存在性)
-
- 1.两数之和 (列表pop(i))
- 383. 赎金信(字典哈希)
- 2404. 出现最频繁的偶数元素
- .贪心算法
-
- 455. 分发饼干(双指针)
- 376. 摆动序列(列表比较pop())
.各位平方求和
while num:
sum_ += (num % 10) ** 2
num = num // 10
.列表转字符串
nums = [1,3,5,6]
target = 5
list_n = [str(v) for v in nums]
s ="".join(list_n)
print(s.find(str(target)))
.反转数字
temp =345
result =int(str(temp)[::-1])
print(result)
temp = 345
result = 0
while temp > 0:
result = result * 10 + temp % 10
temp = temp // 10
print(result)
.列表出现位置
nums =[1,5,3,5,2,4,7]
print([i for i,j in enumerate(nums) if j == 5])
nums .reverse()
index = words.index(element)
print(len(nums ) - index - 1)
.跳出多层循环
flag=True
for i in range(10):
for j in range(10):
if i+j>15 :
print(i, j)
flag=False
break
if not flag:
break
.二分查找
def binarySearch(nums:List[int], target:int) -> int:
left, right = 0, len(nums)-1
while left<=right:
middle = left + (right-left) // 2
if nums[middle] > target:
right = middle - 1
elif nums[middle] < target:
left = middle + 1
else:
return middle
return -1
.移除元素
class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
for i in range(len(nums)-1,-1,-1):
if nums[i]==val:
nums.pop(i)
return len(nums)
.滑动窗口
长度最小的连续子数组
class Solution:
def minSubArrayLen(self, target: int, nums: List[int]) -> int:
res = float("inf")
sum =0
i=0
for j in range(len(nums)):
sum +=nums[j]
while sum>=target:
res = min(res,j-i+1)
sum-=nums[i]
i+=1
return 0 if res== float("inf") else res
.动态规划
53. 最大子数组和
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
dp = [0]*len(nums)
dp[0]=nums[0]
for i in range(1,len(nums)):
dp[i]=max(dp[i-1]+nums[i],nums[i])
return max(dp)
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
result = -float('inf')
count = 0
for i in range(len(nums)):
count += nums[i]
if count > result:
result = count
if count <= 0:
count = 0
return result
300. 最长递增子序列
class Solution:
def lengthOfLIS(self, nums: List[int]) -> int:
l = [1 for i in range(len(nums))]
for i in range(1,len(nums)):
for j in range(i):
if nums[i]>nums[j]:
l[i] = max(l[i],l[j]+1)
return max(l)
674. 最长连续递增序列
class Solution:
def findLengthOfLCIS(self, nums: List[int]) -> int:
res,c=1,1
for i in range(1,len(nums)):
if nums[i]>nums[i-1]:
c+=1
else:
c=1
res = max(res,c)
return res
70. 爬楼梯
class Solution:
def climbStairs(self, n: int) -> int:
if n==0 or n==1:
return 1
if n ==2:
return 2
l=[0]*n
l[0],l[1]=1,2
for i in range(2,n):
l[i]=l[i-1]+l[i-2]
return l[n-1]
746. 使用最小花费爬楼梯
class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
dp=[0]*(len(cost))
dp[0]=cost[0]
dp[1]=cost[1]
for i in range(2,len(cost)):
dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i]
return min(dp[len(cost)-1],dp[len(cost)-2])
416. 分割等和子集(背包)
class Solution:
def canPartition(self, nums: List[int]) -> bool:
target = sum(nums)
if target % 2 == 1: return False
target //= 2
dp = [0] * 10001
for i in range(len(nums)):
for j in range(target, nums[i] - 1, -1):
dp[j] = max(dp[j], dp[j - nums[i]] + nums[i])
return target == dp[target]
198. 打家劫舍
偷窃到的最高金额 = 1 + 3 = 4 。
class Solution:
def rob(self, nums: List[int]) -> int:
if len(nums) == 0:
return 0
if len(nums) == 1:
return nums[0]
dp = [0] * len(nums)
dp[0] = nums[0]
dp[1] = max(nums[0], nums[1])
for i in range(2, len(nums)):
dp[i] = max(dp[i-2]+nums[i], dp[i-1])
return dp[-1]
.哈希(存在性)
1.两数之和 (列表pop(i))
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
for i in range(len(nums)-1,-1,-1):
m = target-nums[i]
nums.pop(i)
if m in nums:
return [i,nums.index(m)]
383. 赎金信(字典哈希)
class Solution:
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
dm={}
dr={}
for v in magazine:
dm[v]=dm.get(v,0)+1
for v in ransomNote:
dr[v]=dr.get(v,0)+1
for k,v in dr.items():
if dm.get(k,0)==0 or dm.get(k)<v:
return 1==0
return 1==1
2404. 出现最频繁的偶数元素
class Solution:
def mostFrequentEven(self, nums: List[int]) -> int:
dt={}
nums.sort()
for v in nums:
if v%2==0:
dt[v]=dt.get(v,0)+1
if dt=={}:
return -1
return max(dt,key=dt.get)
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
n =0
l =[]
for v in s:
if v in l:
l = l[l.index(v)+1:]
l.append(v)
n = len(l) if n<len(l) else n
return n
class Solution:
def lengthOfLongestSubstring(self, s):
st = {}
i, ans = 0, 0
for j in range(len(s)):
if s[j] in st:
i = max(st[s[j]], i)
ans = max(ans, j - i + 1)
st[s[j]] = j + 1
return ans;
.贪心算法
455. 分发饼干(双指针)
class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
g.sort()
s.sort()
start, count = len(s) - 1, 0
for index in range(len(g) - 1, -1, -1):
if start >= 0 and g[index] <= s[start]:
start -= 1
count += 1
return count
376. 摆动序列(列表比较pop())
class Solution:
def wiggleMaxLength(self, nums: List[int]) -> int:
l=[]
for i in range(1,len(nums)):
if nums[i]-nums[i-1]!=0:
l.append(nums[i]-nums[i-1])
for i in range(len(l)-1,0,-1):
if l[i]*l[i-1]>0:
l.pop(i)
return len(l)+1