算法题 (使用python 处理)

博客记录了作者在LeetCode做的算法题,题目是给定一个整数数组,找出数组中两个数的索引,使它们的和等于特定目标值,且假设每个输入只有一个解,同一个元素不能使用两次,并给出了示例。

记录自己leetCode做的算法题

1、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 sameelement twice.

Example:

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

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
def towSum(nums,target):
    indice=0
    flag=True
    while flag:
        length = len(nums)
        if length>1:
           a=nums.pop(0)
           indice += 1
        if length==1:
            print('无法计算得出target' %target)
            break
        for i in range(len(nums)):
            if nums[i] + a==target:
                print('计算得出的下标是%s、%s,值分别是%s+%s=%s'% (indice-1,i+indice,a,nums[i],target))
                flag=False
                break
towSum([1,2,3,4,5,8,9],13)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值