1.Two Sum Leetcode Python

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 your returned answers (both index1 and index2) are not zero-based.

You may assume that each input would have exactly one solution.

Input: numbers={2, 7, 11, 15}, target=9

Output: index1=1, index2=2

这道题目来自于introduction to algorithm书上,介绍的是brute force的用法。

这里我们采用hashtable 将数值即为key value为index 将target-num[index]作为搜索条件。 类似的题目还有4 sum

代码如下:

class Solution:
    # @return a tuple, (index1, index2)
    def twoSum(self, num, target):
        process={}
        for index in range(len(num)):
            if target-num[index] in process:
                return [process[target-num[index]]+1,index+1]
            process[num[index]]=index
时间: 2024-11-09 02:10:36

1.Two Sum Leetcode Python的相关文章

Path Sum leetcode java

题目: 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 true,

[Leetcode]@python 62. Unique Paths

题目链接:https://leetcode.com/problems/unique-paths/ 题目大意:给定n.m,在mxn的矩阵中,从(0,0)走到(m-1,n-1)一共有多少种法(只能往下和往右走) 解题思路:从(0,0)到(m-1,n-1)一共要走m - 1次向下,n-1次向右.也就是在n + m - 2次中选出m-1次向下,也就是C(m + n - 2,m-1) class Solution(object): def uniquePaths(self, m, n): ""&

Pascal's triangle II Leetcode Python

Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space? 这题和前面一题的区别在于不用返回所有解,只需要返回index对应行就行.做法和前面的一样定义一个currow 和prerow class Solu

Combination Sum leetcode java

题目: 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) w

4 Sum leetcode java

题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Note: Elements in a quadruplet (a,b,c,d) must be in non-descending or

3 Sum leetcode java

题目: 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. Note: Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c) The s

Binary Tree Maximum Path Sum leetcode java

题目: Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example: Given the below binary tree, 1 / 2 3 Return 6. 题解: 递归求解. 取当前点和左右边加和,当前点的值中最大的作为本层返回值.并在全局维护一个max.使用数组,因为是引用类型.所以在递归过程中可以保存结果. 代码如下: 1

[Leetcode][Python]40: Combination Sum II

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 40: Combination Sum IIhttps://oj.leetcode.com/problems/combination-sum-ii/ Given a collection of candidate numbers (C) and a target number (T),find all unique combinations in C where the candi

【leetcode?python】Sum Of Two Number

#-*- coding: UTF-8 -*- #既然不能使用加法和减法,那么就用位操作.下面以计算5+4的例子说明如何用位操作实现加法:#1. 用二进制表示两个加数,a=5=0101,b=4=0100:#2. 用and(&)操作得到所有位上的进位carry=0100;#3. 用xor(^)操作找到a和b不同的位,赋值给a,a=0001:#4. 将进位carry左移一位,赋值给b,b=1000:#5. 循环直到进位carry为0,此时得到a=1001,即最后的sum. #!!!!!!关于负数的运算