怎样用python语言编写一个程序,什么都行,求!!!

2025-04-13 03:34:52
推荐回答(2个)
回答1:

给一个列表,里面全是整数。假设给定目标数字A,列表中将有两个整数的和为A,求这两个整数的索引值.

你可以假设每一个输入都只有一个解。

例子

给定列表nums = [2, 7, 11, 15], 目标数字 = 9,

因为 nums[0] + nums[1] = 2 + 7 = 9,
所以 return [0, 1].


class Solution(object):
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        result = [[index1,index2+index1+1] for index1,key1 in enumerate(nums) for index2,key2 in enumerate(nums[index1+1:]) if key1+key2==target]
        return result[0]

回答2:

经典的Hello,world,以下一条语句输出“Hello world!“并换行。
print("Hello,world!\n")