lintcode 中等题:2 Sum 两个数的和

题目

两数之和

给一个整数数组,找到两个数使得他们的和等于一个给定的数target。

你需要实现的函数twoSum需要返回这两个数的下标, 并且第一个下标小于第二个下标。注意这里下标的范围是1到n,不是以0开头。

样例
numbers=[2, 7, 11, 15], target=9

return [1, 2]

注意
你可以假设只有一组答案。

解题

题目之前做LeetCode时候写过这个,利用这里利用ArrayList,当 target- numbers[i] 不在 list中,把numbers[i] 加入到list中,当存在的时候,取出下标,和当前数的下标就是答案了。

public class Solution {
    /*
     * @param numbers : An array of Integer
     * @param target : target = numbers[index1] + numbers[index2]
     * @return : [index1 + 1, index2 + 1] (index1 < index2)
     */
    public int[] twoSum(int[] numbers, int target) {
        // write your code here
        int result[] = new int[2];
        ArrayList<Integer> list = new ArrayList<Integer>();
        for(int i = 0;i<numbers.length ; i++){
            int tmp = target - numbers[i];
            if(!list.contains(tmp)){
                list.add(numbers[i]);
            }else{
                result[0] = list.indexOf(tmp) + 1;
                result[1] = i + 1;
                break;
            }
        }
        return result;
    }
}

Java Code

总耗时: 1042 ms

class Solution:
    """
    @param numbers : An array of Integer
    @param target : target = numbers[index1] + numbers[index2]
    @return : [index1 + 1, index2 + 1] (index1 < index2)
    """
    def twoSum(self, numbers, target):
        # write your code here
        L = {}
        result = [0]*2
        for i in range(len(numbers)):
            tmp = target - numbers[i]
            if tmp not in L:
                L[ numbers[i] ]  = i + 1
            else:
                result[0] = L[tmp]
                result[1] = i + 1
                break
        return result 

Python Code

总耗时: 313 ms

时间: 2024-08-04 17:29:07

lintcode 中等题:2 Sum 两个数的和的相关文章

lintcode 中等题:4 Sum 四个数之和

题目 四数之和 给一个包含n个数的整数数组S,在S中找到所有使得和为给定整数target的四元组(a, b, c, d). 样例 例如,对于给定的整数数组S=[1, 0, -1, 0, -2, 2] 和 target=0. 满足要求的四元组集合为: (-1, 0, 0, 1) (-2, -1, 1, 2) (-2, 0, 0, 2) 注意 四元组(a, b, c, d)中,需要满足a <= b <= c <= d 答案中不可以包含重复的四元组. 解题 怎么感觉下面程序已经没法修改了但是在

lintcode 中等题:next permutation下一个排列

题目 下一个排列 给定一个整数数组来表示排列,找出其之后的一个排列. 样例 给出排列[1,3,2,3],其下一个排列是[1,3,3,2] 给出排列[4,3,2,1],其下一个排列是[1,2,3,4] 注意 排列中可能包含重复的整数 解题 和上一题求上一个排列应该很类似 1.对这个数,先从右到左找到递增序列的前一个位置,peakInd 2.若peakInd = -1 这个数直接逆序就是答案了 3.peakInd>= 0 peakInd这个位置的所,和 peakInd 到nums.size() -1

lintcode 中等题:Single number III 落单的数III

题目 落单的数 III 给出2*n + 2个的数字,除其中两个数字之外其他每个数字均出现两次,找到这两个数字. 样例 给出 [1,2,2,3,4,4,5,3],返回 1和5 挑战 O(n)时间复杂度,O(1)的额外空间复杂度 解题 根据落单的数I,可以想到,所有的数进行异或运行的结果就是所求两个数的异或结果. 这个异或的结果,二进制数是1的位置说明这两个数对应的二进制位不相同.然后再怎么还原???参考,理解的不是很透,找到第k位后,再判断数组中所以数的第k位是0 还是1,,出现两次的数对求解无影

lintcode 中等题:Divide Two Integers 两个数的除法

题目 两个整数相除 将两个整数相除,要求不使用乘法.除法和 mod 运算符. 如果溢出,返回 2147483647 . 样例 给定被除数 = 100 ,除数 = 9,返回 11 解题  15%的通过率,减法,位运算?表示不知道如何下手. 法一:利用减法,超时,人工直接去除的一些情况太流氓. public class Solution { /** * @param dividend the dividend * @param divisor the divisor * @return the re

lintcode 中等题:continuous subarray sum 联系子数组之和

题目 连续子数组求和 给定一个整数数组,请找出一个连续子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的值.(如果两个相同的答案,请返回其中任意一个) 样例 给定 [-3, 1, 3, -3, 4], 返回[1,4]. 解题 法一:直接暴力,时间复杂度O(N2),时间超时 public class Solution { /** * @param A an integer array * @return A list of integers includes the i

lintcode 中等题: 3 Sum II 三数之和II

题目 三数之和 II 给一个包含n个整数的数组S, 找到和与给定整数target最接近的三元组,返回这三个数的和. 样例 例如S = [-1, 2, 1, -4] and target = 1.  和最接近1的三元组是 -1 + 2 + 1 = 2. 注意 只需要返回三元组之和,无需返回三元组本身 解题 和上一题差不多,程序也只是稍微修改了 public class Solution { /** * @param numbers: Give an array numbers of n integ

lintcode 中等题:Intersection of Two Linked Lists 两个链表的交叉

题目 两个链表的交叉 请写一个程序,找到两个单链表最开始的交叉节点. 样例 下列两个链表: A: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 在节点 c1 开始交叉. 注意 如果两个链表没有交叉,返回null. 在返回结果后,两个链表仍须保持原有的结构. 可假定整个链表结构中没有循环. 挑战 需满足 O(n) 时间复杂度,且仅用 O(1) 内存. 解题 尝试用时间复杂度是O(NM),却没有解决,在这个博客看到根据两个链表的特性进行解决. 就如同上图,两个链表相交的部

lintcode 中等题:maximum subarray difference 最大子数组差

题目 最大子数组差 给定一个整数数组,找出两个不重叠的子数组A和B,使两个子数组和的差的绝对值|SUM(A) - SUM(B)|最大. 返回这个最大的差值. 样例 给出数组[1, 2, -3, 1],返回 6 注意 子数组最少包含一个数 挑战 时间复杂度为O(n),空间复杂度为O(n) 解题 刚做了数组中两个子数组和的最大值,这一题是求差,感觉上题的求解思想应该是可以用的 A B 分别是两个子数组的和,则: 所以 当A>B 的时候A越大越好 B越小越好 当A<B 的时候B越大越好 A越小越好

lintcode 中等题:maximum subarray最大子数组II

题目 最大子数组 II 给定一个整数数组,找出两个不重叠子数组使得它们的和最大. 每个子数组的数字在数组中的位置应该是连续的. 返回最大的和. 您在真实的面试中是否遇到过这个题? Yes 样例 给出数组[1, 3, -1, 2, -1, 2],这两个子数组分别为[1, 3]和[2, -1, 2]或者[1, 3, -1, 2]和[2],它们的最大和都是7 注意 子数组最少包含一个数 挑战 要求时间复杂度为O(n) 解题 最大子数组I 这个题目是求一个数组中一个最大子数组的和,而本题目是求数组中的前