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

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

UPDATE (2016/2/13):

The return format had been changed to zero-based indices. Please read the above updated description carefully.

Subscribe to see which companies asked this question

解题分析:

此题目由于是在2月13日有更新,此处需要注意。目前的索引是从0开始的。

思路可以利用key-value模式的方法,key为索引,value为值,这样就可以

利用A : Aindex + B:Bindex = target公式来求出所求索引。

# -*- coding:utf-8 -*-
__author__ = 'jiuzhang'
class Solution(object):
    def twoSum(self, nums, target):
        dict = {}
        for index in xrange(len(nums)):
            if target - nums[index] in dict:
                return [dict[target - nums[index]], index]
            dict[nums[index]] = index
时间: 2024-08-27 10:45:12

(LeetCode)Two Sum --- 求和指定的索引的相关文章

LeetCode: Combination Sum [038]

[题目] Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (including target)

[leetcode]Two Sum @ Python

原题地址:http://oj.leetcode.com/problems/two-sum/ 题意:找出数组numbers中的两个数,它们的和为给定的一个数target,并返回这两个数的索引,注意这里的索引不是数组下标,而是数组下标加1.比如numbers={2,7,11,17}; target=9.那么返回一个元组(1,2).这道题不需要去重,对于每一个target输入,只有一组解,索引要按照大小顺序排列. 解题思路:1,由于要找到符合题意的数组元素的下标,所以先要将原来的数组深拷贝一份,然后排

[LeetCode] K sum(2Sum、3Sum、4Sum)

1 2Sum Given an array of integers, find two numbers such that they add up to a specific target number. 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 not

LeetCode——Two Sum

Given an array of integers, find two numbers such that they add up to a specific target number. 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

LeetCode OJ - Sum Root to Leaf Numbers

这道题也很简单,只要把二叉树按照宽度优先的策略遍历一遍,就可以解决问题,采用递归方法越是简单. 下面是AC代码: 1 /** 2 * Sum Root to Leaf Numbers 3 * 采用递归的方法,宽度遍历 4 */ 5 int result=0; 6 public int sumNumbers(TreeNode root){ 7 8 bFSearch(root,0); 9 return result; 10 } 11 private void bFSearch(TreeNode ro

leetcode -- 3 sum

3-sum 题目描述: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. 题目要求: Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ 

[leetcode]Combination Sum @ Python

原题地址:https://oj.leetcode.com/problems/combination-sum/ 题意: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited

Leetcode:Path Sum 二叉树路径和

Path Sum: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example:Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ 7 2 1 return

LeetCode: Combination Sum II [039]

[题目] Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: All numbers (including target) will be