当前位置:   article > 正文

leetcode 1-10(2024.08.14)

leetcode 1-10(2024.08.14)

立个flag,1-100题每天分配10题,不会就先空着(4,5,8,10)。

1. 1:两数之和

  1. class Solution:
  2. def twoSum(self, nums: List[int], target: int) -> List[int]:
  3. haxi = {}
  4. for i, num in enumerate(nums):
  5. if target - num in haxi:
  6. return [haxi[target - num], i]
  7. else:
  8. haxi[num] = i

2. 2:两数相加

  1. # Definition for singly-linked list.
  2. # class ListNode:
  3. # def __init__(self, val=0, next=None):
  4. # self.val = val
  5. # self.next = next
  6. class Solution:
  7. def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
  8. dummy = res = ListNode()
  9. add = 0
  10. while l1 or l2:
  11. val1 = l1.val if l1 else 0
  12. val2 = l2.val if l2 else 0
  13. val = add + val1 + val2
  14. res.next = ListNode(val % 10)
  15. add = val // 10
  16. l1 = l1.next if l1 else None
  17. l2 = l2.next if l2 else None
  18. res = res.next
  19. if add:
  20. res.next = ListNode(add)
  21. return dummy.next

3. 3:无重复字符的最长子串

  1. class Solution:
  2. def lengthOfLongestSubstring(self, s: str) -> int:
  3. left = 0
  4. right = 0
  5. res = 0
  6. haxi = {}
  7. for i in range(len(s)):
  8. if s[i] in haxi and haxi[s[i]] >= left:
  9. leng = right - left
  10. res = leng if leng > res else res
  11. left = haxi[s[i]] + 1
  12. haxi[s[i]] = i
  13. right = right + 1
  14. leng = right - left
  15. res = leng if leng > res else res
  16. return res

4. 4:寻找两个正序数组的中位数

5. 5:最长回文子串

6. 6:Z字形变换

  1. class Solution:
  2. def convert(self, s: str, numRows: int) -> str:
  3. if numRows == 1: # 行数为1,直接返回原字符串
  4. return s
  5. matrix = []
  6. for i in range(numRows):
  7. matrix.append("")
  8. period = numRows + numRows - 2
  9. idx = 0
  10. num = 0
  11. for st in s:
  12. matrix[idx] = matrix[idx] + st
  13. num = num + 1
  14. if 0 < num % period < numRows:
  15. idx = idx + 1
  16. else:
  17. idx = idx - 1
  18. res = ""
  19. for i in matrix:
  20. res = res + i
  21. return res

7. 7:整数反转

  1. class Solution:
  2. def reverse(self, x: int) -> int:
  3. def get_length(x):
  4. leng = 0
  5. while x:
  6. x = x // 10
  7. leng = leng + 1
  8. return leng
  9. def get_reverse(x, leng):
  10. res = 0
  11. while x:
  12. num = x % 10
  13. res = res + num * 10 ** (leng - 1)
  14. leng = leng - 1
  15. x = x // 10
  16. return res
  17. if x > 0:
  18. leng = get_length(x)
  19. res = get_reverse(x, leng)
  20. elif x == 0:
  21. res = 0
  22. elif x < 0:
  23. leng = get_length(-x)
  24. res = - get_reverse(-x, leng)
  25. return res if -2 ** 31 <= res <= 2 ** 31 - 1 else 0

8. 8. 字符串转换整数 

9. 9:回文数

  1. class Solution:
  2. def isPalindrome(self, x: int) -> bool:
  3. def get_length(x):
  4. leng = 0
  5. while x:
  6. x = x // 10
  7. leng = leng + 1
  8. return leng
  9. def get_reverse(x, leng):
  10. res = 0
  11. while x:
  12. num = x % 10
  13. res = res + num * 10 ** (leng - 1)
  14. leng = leng - 1
  15. x = x // 10
  16. return res
  17. if x > 0:
  18. leng = get_length(x)
  19. res = get_reverse(x, leng)
  20. return res == x
  21. elif x == 0:
  22. return True
  23. elif x < 0:
  24. return False

10. 10:正则表达式匹配

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Guff_9hys/article/detail/990043
推荐阅读
相关标签
  

闽ICP备14008679号