1/50:力扣编程基础从零到一
今天是26/10/29,刚过完生日,之前学的C++和Linux为主,今天开始进军Python,目标是算法岗,冲冲冲!!
class Solution(object): def mergeAlternately(self, word1, word2): #我的第一份练习的代码 ans="" diff=len(word1)-len(word2) #=一比二长 if len(word1)>len(word2): for i in range(len(word2)): ans=ans+word1[i]+word2[i] ans=ans+word1[len(word2):] elif len(word1)==len(word2): for i in range(len(word2)): ans=ans+word1[i]+word2[i] else: for i in range(len(word1)): ans=ans+word1[i]+word2[i] ans=ans+word2[len(word1):] return ans """ :type word1: str :type word2: str :rtype: str """记得ans=""进行初始化
ans=ans[:k]表示取从0到k的字符串(左闭右开),省略表示到端点。
python是elif而不是else if
不用打花括号,是冒号
注意缩进
整数除法是//,单个/返回float
s in (s+s)[1:-1] 是一个判断s是否具有周期性的小技巧
"移动零"类的问题(把一类东西放到list的末尾)可以选择栈方式 stack_size命名直观
1. list的特殊拼接写法
digits=[1]+[0]*(l)因为python里面+会重载,左右都是list就会自动变成拼接模式
2.整数列表的排序法
arr.sort()这么简单就能排序,我在C语言吃的是什么苦
3.判断单调数列的双变量法
class Solution: def isMonotonic(self, nums: List[int]) -> bool: increasing = decreasing = True for i in range(1,len(nums)): if nums[i] > nums[i-1]: decreasing = False if nums[i] < nums[i-1]: increasing = False return increasing or decreasing直接显式的定义布尔值看起来很美观优雅
4.以罗马数字转整数为引的哈希表练习方法
哈希表(字典)就是可迭代的键值对
"可迭代"(iterable)= 能用for一个个取出来的东西
列表、元组、字符串、字典、集合、range、生成器……都是可迭代的。
for x in [1, 2, 3]: # 列表可迭代 for c in "abc": # 字符串可迭代 for k in {'a': 1}: # 字典可迭代(默认取键)这道题的解题代码如下:
这种面对复杂处理请款时定义字典并使用if xx in d的方式检查的方法值得学习
用d.get可以输入多个位数的键来取得值
记住python中的所有切片都是左闭右开的,所以[i,i+2]只能取出两个数字来
class Solution: def romanToInt(self, s: str) -> int: d = {'I': 1, 'IV': 4, 'V': 5, 'IX': 9, 'X': 10, 'XL': 40, 'L': 50, 'XC': 90, 'C': 100, 'CD': 400, 'D': 500, 'CM': 900, 'M': 1000} result = 0 i = 0 while i < len(s): #查看当前位和下一位的字符 str1 = s[i:i+2] #如果当前位置是特殊情况,那么返回其在字典中对应值,并且下一次从特殊字符之后一位开始索引 if str1 in d: result += d.get(str1) i += 2 #如果当前位不是特殊情况,那么只返回当前位的数值 else: result += d[s[i]] i += 1 return result5.用了flag要记得拔!
找字符串中最后一个单词
class Solution: def lengthOfLastWord(self, s: str) -> int: cnt=0 flag=0 for i in range(len(s)): if s[i].isspace(): flag=1 else: if flag==1: cnt=0 flag=0 #之前写代码的时候忘记拔旗了!记得拔! cnt+=1 return cnt别处看到的写法
split能忽视s中的空格返回一个单词list,取[-1]取出最后一然后求长度就可以
class Solution: def lengthOfLastWord(self, s: str) -> int: return len(s.split()[-1])6.字符串转大小写的方法
s.upper() # 整个字符串转大写 s.lower() # 整个字符串转小写7.棒球比赛——第一个手写的栈!
stack.append()压入栈顶
stack.pop()出栈
stack[-1]指向最后一个
8.机器人回原点——两种方法均值的学习!
下面这种方法使用的moves.count能计算出list中每个元素的值
class Solution: def judgeCircle(self, moves: str) -> bool: return moves.count('R') == moves.count('L') and \ moves.count('U') == moves.count('D')下面这种方法使用了字典,并且利用dx,dy=d[ch]取出了字典中的值
class Solution: def judgeCircle(self, moves: str) -> bool: point = [0,0] d={"U":[0,1],"D":[0,-1],"L":[-1,0],"R":[1,0]} for ch in moves: dx,dy=d[ch] point[0]+=dx point[1]+=dy if point==[0,0]: return True return False或者也可以使用Counter函数建立字典
class Solution: def judgeCircle(self, moves: str) -> bool: cnt = Counter(moves) return cnt['R'] == cnt['L'] and cnt['U'] == cnt['D']9.逻辑最复杂的一集——井字棋的获胜者
我的写法如下
class Solution: def tictactoe(self, moves: list[list[int]]) -> str: Awin=0 Bwin=0 grid = [[''] * 3 for _ in range(3)] for i in range (len(moves)): dx,dy=moves[i] if i%2==0: grid[dx][dy]='A' for j in range(3): if grid[j][0]==grid[j][1]==grid[j][2]=='A' or grid[0][j]==grid[1][j]==grid[2][j]=='A' or grid[0][0]==grid[1][1]==grid[2][2]=='A' or grid[0][2]==grid[1][1]==grid[2][0]=='A': return "A" else: grid[dx][dy]='B' for j in range(3): if grid[j][0]==grid[j][1]==grid[j][2]=='B' or grid[0][j]==grid[1][j]==grid[2][j]=='B' or grid[0][0]==grid[1][1]==grid[2][2]=='B' or grid[0][2]==grid[1][1]==grid[2][0]=='B': return "B" if len(moves)==9: return"Draw" return"Pending" #比较大小输出结果10.登堂入室——解决第一个中等题:困于环中的机器人
但是代码不难,跳过
矩阵专题!共四题
1.最富有客户
class Solution: def maximumWealth(self, accounts: List[List[int]]) -> int: arr = [sum(row) for row in accounts] return max(arr)map初见:map是一个迭代器
map(函数, 可迭代对象)的作用是:对可迭代对象里的每个元素,都调用一次这个函数,把结果逐个产出。
map(sum,accounts)是求每行相加
2.对角线元素的和
与1按位与(&1)可以判断奇偶