首先先把特例干掉
输入为两个字符串,输出最长特殊序列的长度。如果不存在,则返回 -1。
其次按照题目描述走
class Solution:
def findLUSlength(self, a: str, b: str) -> int:
if a == b :
return -1
return max(len(a),len(b))
用切片工具进反转
class Solution(object):
def isPalindrome(self, x):
"""
:type x: int
:rtype: bool
"""
if str(x)[::-1] == str(x)[:]:
return True
else:
return False
nums
,编写一个函数将所有 0
移动到数组的末尾,同时保持非零元素的相对顺序。
示例:
输入: [0,1,0,3,12] 输出: [1,3,12,0,0]
说明:
我的思路是先记录0的数量,把0删光,再根据计数0的个数末尾加上去
class Solution:
def moveZeroes(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
num_0 = 0
while 0 in nums:
index_0 = nums.index(0)
nums.pop(index_0)
num_0 += 1
for a in range(num_0):
nums.append(0)
有更加好的思路,或者解题方法评论区留言谢谢