`
hellobin
  • 浏览: 65834 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

1. Two Sum

 
阅读更多

 

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++-leetcode题解之001. Two Sum.cpp

    c++ C++_leetcode题解之001. Two Sum.cpp

    js代码-1. Two Sum

    【压缩包子文件的文件名称列表】中的`main.js`通常包含实际的程序代码,即上面提供的`twoSum`函数的实现。`README.txt`文件通常用于提供项目说明,包括如何运行代码、项目目的等信息,但在这里没有具体的内容,因此...

    程序员面试宝典LeetCode刷题手册

    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 ...

    LeetCode 1.Two Sum

    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 ...

    Go 零基础2000题 从入门到精通

    说明 ⽬录 第⼀章 序章 关于 LeetCode 什么是 Cookbook 为什么会写这个开源书 关于书的封⾯ 关于作者 ...1. Two Sum 2. Add Two Numbers 3. Longest Substring Without Repeating Characters 4. ......

    Two Sum算法调试小demo

    在这个`TwoSum.java`文件中,我们定义了一个名为`TwoSum`的类,包含一个`twoSum`方法来解决Two Sum问题。`main`方法用于测试,给出了一个示例输入数组`nums`和目标值`target`,并打印出结果。 在调试过程中,我们...

    leetcode2sumc-Two-Sum:我的LeetCodeC解决方案:二和

    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...

    twoSum0.zip

    print(twoSum(nums, target)) # 输出: [0, 1] ``` 以上就是"twoSum0.py"文件可能包含的内容,它展示了如何用Python解决两数之和问题。这个程序通过利用哈希表的特性,可以在O(n)的时间复杂度内找到答案,大大提高了...

    Two Sum leetcode c++

    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)...

    leetcode1004-leetcode:leetcode

    1. Two Sum (E) 167. Two Sum II - Input array is sorted (E) 653. Two Sum IV - Input is a BST (E) -&gt; 2 26. Remove Duplicates from Sorted Array (E) 27. Remove Element (E) 31. Next Permutation (M) * -&gt; ...

    lrucacheleetcode-leetcode:leetcode

    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 ...

    1 two sum_SUM_leetcodec++代码_

    Leetcode 1 two sum C++ code

    leetcode打不开-leetcode:leetcode

    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

    twoSum问题的核心思想.md

    leetcode2sumc-leetcode:leetcode使用单元测试框架练习

    Two Sum " 生成的文件结构如下: $id. $problem_title ├── solution.hpp # implement the solution └── TEST.cpp # UNIT Testcases 现在。 编写用于测试和解决方案的代码! /* * 1. Two Sum/TEST.cpp * */ # ...

    leetcode338-LeetCode:LeetCode刷题总结

    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-leetcode题解之1-two-sum.js

    js js_leetcode题解之1-two-sum.js

    lrucacheleetcode-Algorithm:一些常见的算法的解题报告

    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 two sum java 解法

Global site tag (gtag.js) - Google Analytics