002 两数相加
2. 两数相加(中等)
Python
class Solution:
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
if not l1:
return l2
if not l2:
return l1
dummyhead = ListNode(-1)
p = dummyhead
f = 0
while l1 or l2 or f:
s = (l1.val if l1 else 0) + (l2.val if l2 else 0) + f
num = s%10
f = s//10
p.next = ListNode(num)
p = p.next
l1 = l1.next if l1 else None
l2 = l2.next if l2 else None
return dummyhead.next
C++
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode *dummyhead = new ListNode(-1);
ListNode *p = dummyhead;
int flag = 0;
while(l1!=NULL or l2!=NULL or flag!=0){
int s = 0;
if (l1!=NULL) {
s+=l1->val;
l1 = l1->next;
}
if (l2!=NULL) {
s+=l2->val;
l2 = l2->next;
}
s += flag;
int num=s%10;
flag = s/10;
p->next = new ListNode(num);
p = p->next;
}
return dummyhead->next;
}
};
445 两数相加II
相似题目
445. 两数相加 II(中等)
对链表进行翻转后,则可以用2的方法;
进阶:考虑栈(先进后出)
class Solution:
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
stack1 = []
while l1:
stack1.append(l1.val)
l1 = l1.next
stack2 = []
while l2:
stack2.append(l2.val)
l2 = l2.next
flag = 0
res = []
while stack1 or stack2 or flag:
s = (stack1.pop() if stack1 else 0) + (stack2.pop() if stack2 else 0) +flag
flag = s//10
s = s%10
res.append(s)
dummyhead = ListNode(-1)
p = dummyhead
while res:
p.next = ListNode(res.pop())
p = p.next
return dummyhead.next
004 寻找两个正序数组的中位数
4. 寻找两个正序数组的中位数(困难)
方法一:将两个数组合并为一个有序数组
方法二:二分策略
TODO
005 最长回文子串
5. 最长回文子串(中等)
方法一:暴力法
超时
方法二:动态规划
s[i][j]表示s[i:j+1]的字符串,前闭后闭
二维矩阵dp记录,dp[i][j]表示s[i][j]是否为回文串
s[i][j]为回文串的几种情况
j=i 时,一定为回文串
j-i=1和j-i=2时,如果s[i]==s[j]则为回文串
j-i>2时,s[i]==s[j]且s[i+1][j-1](去除头尾的中间部分)为回文串时,回文
class Solution:
def longestPalindrome(self, s: str) -> str:
if len(s)<=1:
return s
n = len(s)
dp = [[False for _ in range(n)]for _ in range(n)]
max_len = 1#记录最长的长度
start = 0 #记录最长回文子串的初始位置
for j in range(1,n):
for i in range(j):
if s[i]==s[j]:
if j-i<3:
dp[i][j]=True
else:
dp[i][j] = dp[i+1][j-1]
if dp[i][j]:
cur_len = j-i+1
if cur_len>max_len:
max_len = cur_len
start = i
return s[start:start+max_len]
007 整数反转
7.整数反转(简单)
Python: 存储数字理论上是无限长度,因此每次计算完后判断res与of的大小关系即可;
如果是在其他语言中,如Java,则 数字计算会溢出,因此要判断res和of / 10的大小关系(即确定再添 11 位是否会溢出)
1<<31 = 2**31
class Solution:
def reverse(self, x: int) -> int:
y,res = abs(x),0
of = (1<<31)-1 if x>0 else 1<<31
while y!=0:
res = res*10+y%10
if res>of: return 0
y//=10
return res if x>0 else -res
008 字符串转换整数 (atoi)
8.字符串转换整数 (atoi)(中等)
很啰嗦的解法
class Solution:
def myAtoi(self, s: str) -> int:
INT_MAX=2**31-1
INT_MIN=-2**31
n = len(s)
i = 0
while i<n and s[i]==' ':
i += 1
if n==0 or i==n:
return 0
flag = False
res = 0
if not s[i].isdigit():
if s[i]=='-':
flag = True
i += 1
elif s[i]=='+':
i += 1
else:
return 0
while i<n:
c = s[i]
if c.isdigit():
res = res*10+int(c)
else:
break
i += 1
if flag==True:
res = -1*res
if res<INT_MIN:
return INT_MIN
else:
if res>INT_MAX:
return INT_MAX
return res
009 回文数
9.回文数(简单)
方法一:转为字符串再判断
class Solution:
def isPalindrome(self, x: int) -> bool:
if x<0:
return False
s = str(x)
l = 0
r = len(s)-1
while l<r:
if s[l]!=s[r]:
return False
l += 1
r -= 1
return True
方法二:通过取整和取余求首位和末位
class Solution:
def isPalindrome(self, x: int) -> bool:
if x<0:
return False
bit = 1
while x//bit>=10:
bit = bit*10
while x>0:
left = x//bit
right = x%10
if left!=right:
return False
x = (x%bit)//10
bit = bit//100
return True
x//bit 取首位
x%10 取末位
x%bit 除首位,再//10除末位
方法三:取后半段数字进行翻转
注意:回文数的位数可奇可偶
末位为0则可以直接返回False(注意0%10==0,但0是回文数)
class Solution:
def isPalindrome(self, x: int) -> bool:
if x<0 or (x%10==0 and x!=0):
return False
res = 0
while x>res:
y = x%10
res = res*10+y
x = x//10
return x==res or x==res//10
参考资料:
Datawhale资料
https://leetcode-cn.com/problems/reverse-integer/solution/reverse-integer-by-jin407891080/
https://leetcode-cn.com/problems/palindrome-number/solution/dong-hua-hui-wen-shu-de-san-chong-jie-fa-fa-jie-ch/