链表

反转链表(力扣70)

思路1:转字符串之后判断

1
2
3
class Solution:
def isPalindrome(self, x: int) -> bool:
return str(x) == str(x)[::-1]

思路2:通过数字判断(这里要注意x发生变化了,最后判断时需要与原始的x进行比较)

1
2
3
4
5
6
7
8
9
10
class Solution:
def isPalindrome(self, x: int) -> bool:
before = x
if x < 0:
return False
res = 0
while x>0:
res = res*10 + x%10
x = x//10
return res == before

最小花费爬楼梯(力扣746)

思路:字典判断个数即可,小于等于1个则返回True

1
2
3
4
5
6
7
8
9
from collections import Counter
class Solution:
def canPermutePalindrome(self, s: str) -> bool:
temp = Counter(s)
count = 0
for i in temp:
if temp[i]%2 == 1:
count += 1
return count <= 1

参考