1. Two Sum
Easy
17485626Add to ListShare
Given an array of integers nums
and an integer target
, return indices of the two numbers such that they add up to target
.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
You can return the answer in any order.
Example 1:
Input: nums = [2,7,11,15], target = 9 Output: [0,1] Output: Because nums[0] + nums[1] == 9, we return [0, 1].
Example 2:
Input: nums = [3,2,4], target = 6 Output: [1,2]
Example 3:
Input: nums = [3,3], target = 6 Output: [0,1]
Constraints:
2 <= nums.length <= 105
-109 <= nums[i] <= 109
-109 <= target <= 109
- Only one valid answer exists.
class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: # number -> index map = {} for i, num in enumerate(nums): remain = target - num if remain in map: return [i, map[remain]] else: map[num] = i
知识点:
1 enumerate
2 hashmap
相关推荐
c++ C++_leetcode题解之001. Two Sum.cpp
【压缩包子文件的文件名称列表】中的`main.js`通常包含实际的程序代码,即上面提供的`twoSum`函数的实现。`README.txt`文件通常用于提供项目说明,包括如何运行代码、项目目的等信息,但在这里没有具体的内容,因此...
1. Two Sum 2. Add Two Numbers 3. Longest Substring Without Repeating Characters 4. Median of Two Sorted Arrays 7. Reverse Integer 9. Palindrome Number 11. Container With Most Water 13. Roman to ...
Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the same ...
说明 ⽬录 第⼀章 序章 关于 LeetCode 什么是 Cookbook 为什么会写这个开源书 关于书的封⾯ 关于作者 ...1. Two Sum 2. Add Two Numbers 3. Longest Substring Without Repeating Characters 4. ......
在这个`TwoSum.java`文件中,我们定义了一个名为`TwoSum`的类,包含一个`twoSum`方法来解决Two Sum问题。`main`方法用于测试,给出了一个示例输入数组`nums`和目标值`target`,并打印出结果。 在调试过程中,我们...
Sum. Memory Usage: 5.8 MB, less than 99.36% of C online submissions for Two Sum. #1. Two Sum Given an array of integers nums and an integer target, return indices of the two numbers such that they add...
print(twoSum(nums, target)) # 输出: [0, 1] ``` 以上就是"twoSum0.py"文件可能包含的内容,它展示了如何用Python解决两数之和问题。这个程序通过利用哈希表的特性,可以在O(n)的时间复杂度内找到答案,大大提高了...
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2)...
1. Two Sum (E) 167. Two Sum II - Input array is sorted (E) 653. Two Sum IV - Input is a BST (E) -> 2 26. Remove Duplicates from Sorted Array (E) 27. Remove Element (E) 31. Next Permutation (M) * -> ...
1. Two Sum 2. Add Two Numbers 3. Longest Substring Without Repeating Characters 4. Median of Two Sorted Arrays 5. Longest Palindromic Substring 7. Reverse Integer 9. Palindrome Number 11. Container ...
Leetcode 1 two sum C++ code
Tip1: Two pointer for sorted array (#Array 1. Two Sum) Tip2: Sum[i:j] = Sum[0:j] - Sum[0:i] for continuous array (# Array 560. Subarray Sum Equals K) Tip3: Knapsack Problem (0/1, unbounded) (#DP 322. ...
twoSum问题的核心思想.md
Two Sum " 生成的文件结构如下: $id. $problem_title ├── solution.hpp # implement the solution └── TEST.cpp # UNIT Testcases 现在。 编写用于测试和解决方案的代码! /* * 1. Two Sum/TEST.cpp * */ # ...
1.Two Sum 2.Add Two Numbers 3.Longest Substring Without Repeating Characters 4.Median of Two Sorted Arrays 5.Longest Palindromic Substring (Manacher算法待完成) 6.ZigZag Conversion 7.Reverse Integer 8....
js js_leetcode题解之1-two-sum.js
1.Two Sum 2.Add Two Numbers 3.Longest Substring Without Repeating Characters 4.Median of Two Sorted Arrays 7.Reverse Integer 8.String to Integer (atoi) 9.Palindrome Number 11.Container With Most Water...
Leetcode two sum java 解法