标签:__ 10 ListNode target 每天 next l2 l1 leetcode
1、找到 a
,找到 b
,让 a + b = target
class Solution: def twoSum(self, nums, target): """ :type nums: List[int] :type target: int :rtype: List[int] """ dic = {} for index, num in enumerate(nums): if num in dic: return [dic[num], index] dic[target - num] = index print(dic) if __name__ == "__main__": nums = [2, 7, 11, 15] target = 9 assert (Solution().twoSum(nums, target) == [0, 1]) # print(Solution().twoSum(nums, target)) # nums = [3, 2, 4] # target = 6 # assert (Solution().twoSum(nums, target) == [1, 2])
class ListNode: def __init__(self, x): self.val = x self.next = None class Solution: def addTwoNumbers(self, l1, l2): """ :type l1: ListNode :type l2: ListNode :rtype: ListNode """ head = ListNode(0) p = head quot = 0 while l1 or l2 or quot != 0: if l1: quot += l1.val l1 = l1.next if l2: quot += l2.val l2 = l2.next quot, rem = divmod(quot, 10) #return the tuple (x//y, x%y) print(quot,rem) p.next = ListNode(rem) p = p.next return head.next def compareLinkedList(l1, l2): while l1 or l2: if not (l1 and l2) or l1.val != l2.val: return False l1 = l1.next l2 = l2.next return True if __name__ == "__main__": l1 = ListNode(2) l1.next = ListNode(4) l1.next.next = ListNode(3) l2 = ListNode(5) l2.next = ListNode(6) l2.next.next = ListNode(4) lsum = ListNode(7) lsum.next = ListNode(0) lsum.next.next = ListNode(8) print(compareLinkedList(Solution().addTwoNumbers(l1, l2), lsum))
3、没有重复字符的最长子字符串的长度
标签:__,10,ListNode,target,每天,next,l2,l1,leetcode 来源: https://blog.csdn.net/weixin_38740463/article/details/94640820
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。