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
相关推荐
js代码-1. Two Sum
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 ...
Two Sum算法调试小demo
twoSum0.zip
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...
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....
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 解法
答案LeetCode_1_TwoSum LeetCode 问题:给定一个整数数组,找出两个数字,使它们相加为特定的目标数字。 函数 twoSum 应该返回两个数字的索引,使它们相加为目标,其中 index1 必须小于 index2。 请注意,您返回的...
窗口总和610.TwoSum-差等于目标228.链表中间464.排序整数II 5.Kth最大Elemenet 607.Two Sum III-数据结构设计539.移动零点608.二次加和II 102.链表周期103.LinkedList周期II 380.两个链表的交集148.颜色排序894.煎饼...
华为 leetcode mistake-collection This is mistake ...Sum code_4.org Add Two Numbers code_5.org Longest Substring Without Repeating Characters code_6.org 二进制链表转整数 code_7.org 最小高度树