自我监督刷题记录处4
第4天。我订购了某位大神土著作者的python辅导账号,希望我能坚持看完所有他的文章。。之后。。。能写点诗句,摆脱python八股文的模式。> k <…非常抱歉现在只能写不好看的代码,我尽量努力🤐
Leetcode 189 Rotate Image
(Medium Level)
class Solution:
def rotate(self, matrix: List[List[int]]) -> None:
"""
Do not return anything, modify matrix in-place instead.
"""
n= len(matrix)
for i in range (n):
for j in range(i,n):
#先转置
matrix[i][j],matrix[j][i] = matrix[j][i],matrix[i][j]
#后水平翻转
for i in matrix:
i.reverse()
2048也可以用同样的转置方法来做。
Leetcode 136 Single Number
(Easy Level)
class Solution:
def singleNumber(self, nums: List[int]) -> int:
a = 0
for i in nums:
a = a ^ i
return a
不看评论想不到用异或,但要求是不使用额外空间。挺有趣,mark一下
Leetcode 350 Intersection of Two Arrays II
(Easy Level)
class Solution:
def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]:
l1 = len(nums1)
l2 = len(nums2)
list_ = []
if l1 ==0 or l2 ==0:return []
#
for i in range (l1):
if nums1[i] in nums2:
l.append(nums1[i])
nums2.remove(nums1[i])
return list_
题目比较简单,我觉得的想法是比较直接的,但看了解题基本都是用排序就是指针,比我写的好看,但我的比较好想到。
今天开始要试着刷土著的语感100道,希望能有帮助
来源:CSDN
作者:DeepPoor
链接:https://blog.csdn.net/kellywow/article/details/104573753