
LeetCode登录问题的解决之道:从障碍到答案!
5星
- 浏览量: 0
- 大小:None
- 文件类型:None
简介:
本文提供了针对LeetCode平台登录过程中遇到的各种问题的有效解决方案,帮助用户顺利克服障碍并成功登录。
LeetCode无法登录力码1. 二和给定一个整数数组,返回两个数字的索引,使它们相加为特定目标值。(可以假设每个输入都只有一个解决方案,并且您不能两次使用相同的元素。)例如:Given nums=[2,7,11,15], target=9 因为 nums[0]+nums[1]=2+7=9 所以返回 [0, 1]。
O(n) 解决方案如下:
```python
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
if len(nums)<=1:
return False
buff_dict = {}
for i in range(len(nums)):
if nums[i] in buff_dict:
return [buff_dict[nums[i]], i]
else:
buff_dict[target-nums[i]] = i
```
2. 两个数相加:给定两个表示非负整数的数字,重写如下:
```python
def addTwoNumbers(l1: ListNode, l2: ListNode) -> ListNode:
carry = 0
dummy_head = ListNode(0)
current = dummy_head
while l1 != None or l2 != None or carry == 1:
if l1 != None:
carry += l1.val
l1 = l1.next
if l2 != None:
carry += l2.val
l2 = l2.next
current.next = ListNode(carry % 10)
current = current.next
carry //= 10
return dummy_head.next
```
全部评论 (0)


