Vorwort:博主非科班出身,本科在德国某不知名高校就读机械制造,实习是在德国某厂做测试开发(只懂些基础,努力Sprint中.)Ps:该开始实习... 自我Pua中...
Haupt:再做题目前,我查阅了不少资料:比如该如何做题,什么顺序,有什么前置知识积累。
但是总结起来就是:因人而异。
我的计划如下:从easy开始做,然后按顺序来,及时总结,然后定期整理分类总结。
PS:重要的是思路和对基础方法的最优选择
所有代码都是自己的敲的(除了标注了的,或者提示段),有错误,或者不优雅的地方欢迎DM。
Lc0001:
我的代码实现:
class Solution:
def twoSum(self, nums, target):
self.shuchu=[]
for i in range(len(nums)-1):
for x in range(i+1,len(nums)):
if nums[i]+nums[x]==target:
self.shuchu.append(i)
self.shuchu.append(x)
return self.shuchu
这个是我初始想法 通过range(len() )遍历然后 每个进行匹配判断,但是对于元素非常多list效果不太行,运行速度不够快,不够简洁。
然后从评论中找到一个方法
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
dict = {}
for i in range(len(nums)):
if target-nums[i] in dict:
return [dict[target-nums[i]], i]
else:
dict[nums[i]] = i
恍然大悟。
只要将每个元素对target相减 判断是否存在在List中,能够节省许多的时间。
Lc0009:
class Solution:
def isPalindrome(self, x: int) -> bool:
str_x=str(x)
rev_x=str_x[::-1]
if str_x==rev_x:
return True
else:
return False
这个用到了Str 的切片,翻转证明回文数 不多解释
Lc0013:
class Solution:
def romanToInt(self, s: str) -> int:
Sum=s.count('M')*1000+s.count("D")*500+s.count('C')*100+s.count('L')*50+s.count("X")*10+s.count("V")*5+s.count("I")
Abzug= s.count('CM')*200+s.count("CD")*200+s.count('XL')*20+s.count("XC")*20+s.count('IV')*2+s.count("IX")*2
Sum-=Abzug
return Sum
暴力不优雅.
思路:将所有的罗马字母先原本含义计算 如果出现CM之类 反而会减少Sum的,则单独拉出来计算。
Lc0014
class Solution:
def longestCommonPrefix(self, strs) -> str:
ord_strs = sorted(strs, key=lambda x: len(x))
if len(ord_strs) == 1:
return ord_strs[0]
else:
if ord_strs[0]=='':
return ''
else:
for i in range(len(ord_strs[0])):
for x in range(len(ord_strs)):
defi = ord_strs[0][i]
if ord_strs[x][i] != defi:
if i > 0:
return ord_strs[0][:i]
else:
return ''
return ord_strs[0]
思路:首先我们应该整理list的内容顺序.要求:字符串长度从小到大,所以用了x:len(x)的 常规的匿名函数为key 进行整理。
这道题要考虑到2种额外情况,当list只有一个元素时以及当list存在元素' '情况下要返回唯一的元素或者返回'',没定义的话会返回None. 那就不优雅了
Lc0020:
我的代码太过丑陋就不便展示。
下面是讨论区的方法
class Solution:
def isValid(self, s: str) -> bool:
stack = []
mapping = {
'(': ')',
'[': ']',
'{': '}'
}
for char in s:
if char in mapping.keys():
stack.append(mapping[char])
elif stack==[] or stack[-1]!=char:#or use ##elif not stack
return False
else:
stack.pop()
return len(stack)==0
mapping这个字典的方法真的是绝了,有点美
后面的话就是运用了后进先出的堆栈思想.
Lc0026
非常有意思的一道题,非常考验你对Python 传参的理解
参考链接:python:函数传参是否会改变函数外参数的值_夏洛的网的博客-CSDN博客
有且仅有 list 和 dic 才会改变函数外传参的
因为要求他返回时Int 所以我们必须通过nums内置方法 函数来改变其值,不能直接赋值。
我的代码
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
nums_ord = sorted(set(nums),key=nums.index)
nums.clear()
nums.extend(self.nums)
return len(nums)
还有简单的实现方式可以看leetcode的Solution.
例如
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
i=0
for j in nums:
if j not in nums[:i]:
nums[i]=j
i+=1
return i
Lc0027
没啥好说的 可以加法,可以减法.
class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
i=nums.count(val)
for j in range(i):
nums.remove(val)
j+=1
return len(nums)
Lc0035
基本方法遍历
class Solution:
def searchInsert(self, nums: List[int], target: int) -> int:
for i in range(len(nums)):
if nums[i] >= target:
return i
return len(nums)
或者直接加入然后整理,在查找index
class Solution:
def searchInsert(self, nums: List[int], target: int) -> int:
if target not in nums:
nums.append(target)
nums.sort()
return nums.index(target)
Ps:target 在的话 直接求nums.index(target)一样的