class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
numLength = len(nums)
for i in range(numLength):
for j in range(i+1,numLength):
if nums[i]+nums[j] == target:
return [i,j]
return []
# 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]:
h1 = ListNode(-1)
n1, n2, n3 = l1, l2, h1
remain,curSum = 0,0
while n1 or n2:
curSum = 0
if n1:
curSum += n1.val
n1 = n1.next
if n2:
curSum += n2.val
n2 = n2.next
curSum += remain
cur = ListNode(curSum%10)
remain = curSum//10
n3.next = cur
n3 = n3.next
if remain:
cur = ListNode(remain)
n3.next = cur
return h1.next
时间复杂度:O(m+n)
空间复杂度:O(1)
3.Longest Substring Without Repeating Characters
使用字典记录历史位置
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
usedChars = {}
maxLen,begin = 0,0
for i,char in enumerate(s):
if char in usedChars:
begin =max(usedChars[char]+1,begin)
usedChars[char] = i
maxLen = max(maxLen,i-begin+1)
return maxLen
274

被折叠的 条评论
为什么被折叠?



