赞
踩
立个flag,1-100题每天分配10题,不会就先空着(4,5,8,10)。
1. 1:两数之和
- class Solution:
- def twoSum(self, nums: List[int], target: int) -> List[int]:
- haxi = {}
- for i, num in enumerate(nums):
- if target - num in haxi:
- return [haxi[target - num], i]
- else:
- haxi[num] = i
2. 2:两数相加
- # Definition for singly-linked list.
- # class ListNode:
- # def __init__(self, val=0, next=None):
- # self.val = val
- # self.next = next
- class Solution:
- def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
- dummy = res = ListNode()
- add = 0
- while l1 or l2:
- val1 = l1.val if l1 else 0
- val2 = l2.val if l2 else 0
- val = add + val1 + val2
- res.next = ListNode(val % 10)
- add = val // 10
- l1 = l1.next if l1 else None
- l2 = l2.next if l2 else None
- res = res.next
- if add:
- res.next = ListNode(add)
- return dummy.next
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
3. 3:无重复字符的最长子串
- class Solution:
- def lengthOfLongestSubstring(self, s: str) -> int:
- left = 0
- right = 0
- res = 0
- haxi = {}
- for i in range(len(s)):
- if s[i] in haxi and haxi[s[i]] >= left:
- leng = right - left
- res = leng if leng > res else res
- left = haxi[s[i]] + 1
- haxi[s[i]] = i
- right = right + 1
- leng = right - left
- res = leng if leng > res else res
- return res
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
5. 5:最长回文子串
6. 6:Z字形变换
- class Solution:
- def convert(self, s: str, numRows: int) -> str:
- if numRows == 1: # 行数为1,直接返回原字符串
- return s
- matrix = []
- for i in range(numRows):
- matrix.append("")
- period = numRows + numRows - 2
- idx = 0
- num = 0
- for st in s:
- matrix[idx] = matrix[idx] + st
- num = num + 1
- if 0 < num % period < numRows:
- idx = idx + 1
- else:
- idx = idx - 1
- res = ""
- for i in matrix:
- res = res + i
- return res
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
7. 7:整数反转
- class Solution:
- def reverse(self, x: int) -> int:
- def get_length(x):
- leng = 0
- while x:
- x = x // 10
- leng = leng + 1
- return leng
- def get_reverse(x, leng):
- res = 0
- while x:
- num = x % 10
- res = res + num * 10 ** (leng - 1)
- leng = leng - 1
- x = x // 10
- return res
-
- if x > 0:
- leng = get_length(x)
- res = get_reverse(x, leng)
- elif x == 0:
- res = 0
- elif x < 0:
- leng = get_length(-x)
- res = - get_reverse(-x, leng)
- return res if -2 ** 31 <= res <= 2 ** 31 - 1 else 0
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
8. 8. 字符串转换整数
9. 9:回文数
- class Solution:
- def isPalindrome(self, x: int) -> bool:
- def get_length(x):
- leng = 0
- while x:
- x = x // 10
- leng = leng + 1
- return leng
- def get_reverse(x, leng):
- res = 0
- while x:
- num = x % 10
- res = res + num * 10 ** (leng - 1)
- leng = leng - 1
- x = x // 10
- return res
-
- if x > 0:
- leng = get_length(x)
- res = get_reverse(x, leng)
- return res == x
- elif x == 0:
- return True
- elif x < 0:
- return False
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
10. 10:正则表达式匹配
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。