zoukankan      html  css  js  c++  java
  • 两数之和

    题目描述

    给出一个整数数组,请在数组中找出两个加起来等于目标值的数,
    你给出的函数twoSum 需要返回这两个数字的下标(index1,index2),需要满足 index1 小于index2.。注意:下标是从1开始的
    假设给出的数组中只存在唯一解
    例如:

    给出的数组为 {20, 70, 110, 150},目标值为90
    输出 index1=1, index2=2

    示例1

    输入
    [3,2,4],6
    返回值
    [2,3]

    代码

    class Solution:
        def twoSum(self, numbers, target):
            for index in range(len(numbers)):
                for index2 in range(1, len(numbers)):
                    if (numbers[index]+numbers[index2]) == target:
                        if index+1==index2+1:
                            continue
                        return [index+1, index2+1]
    
    
    if __name__ == "__main__":
        s = Solution()
        result = s.twoSum([3, 2, 4], 6)
        print(result)
    不论你在什么时候开始,重要的是开始之后就不要停止。 不论你在什么时候结束,重要的是结束之后就不要悔恨。
  • 相关阅读:
    Codeforces Round #534 (Div. 2) D. Game with modulo 交互题
    传球游戏 dp
    欧拉通路和欧拉回路
    HDU 1116
    HDU 4970
    HDU 4557
    HDU 4864
    HDU 1565
    HDU 3046
    HDU 4240
  • 原文地址:https://www.cnblogs.com/yunhgu/p/13896622.html
Copyright © 2011-2022 走看看