永发信息网

给个数组,找出其中四个数使得其之和为给定的一个目标值,用python

答案:1  悬赏:70  手机版
解决时间 2021-11-29 10:46
  • 提问者网友:ミ烙印ゝ
  • 2021-11-28 16:48
给个数组,找出其中四个数使得其之和为给定的一个目标值,用python
最佳答案
  • 五星知识达人网友:独行浪子会拥风
  • 2021-11-28 17:33
class Solution(object):
    def fourSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[List[int]]
        """
        length = len(nums);
        result = [];
        nums.sort();                                            #先排序
        first = 0;
        while first < length - 3:                               #第一个数最右的位置也只能到倒数第四
            second = first + 1;
            while second < length - 2:                          #第二个数最右的位置也只能到倒数第三
                third = second + 1;
                fourth = length - 1;
                sum12 = nums[first] + nums[second];
                while third < fourth:
                    sum = sum12 + nums[third] + nums[fourth];
                    error = target - sum;
                    if error == 0:                              #如果四个数之和刚好等于目标值,加入结果list
                        result.append([nums[first],nums[second],nums[third],nums[fourth]]);
                        while third < length - 1 and nums[third] == nums[third + 1]:
                            third += 1;
                        third += 1;                             #将第三个数的索引移动下一个不一样的数上去
                        while second < length - 1 and nums[second] == nums[second + 1]:
                            second += 1;                        #将第二个数的索引移动到最后一个一样的数上
                        while first < length - 1 and nums[first] == nums[first + 1]:
                            first += 1;                         #将第一个数的索引移动到最后一个一样的数上
                    elif error > 0:                             #当四个数之和小于目标值,第三个数的索引右移到一个不同的数上
                        while third < length - 1 and nums[third] == nums[third + 1]:
                            third += 1;
                        third += 1;
                    else:                                       #当四个数之和大于目标值,第四个数的索引左移到一个不同的数上
                        while fourth > 0 and nums[fourth] == nums[fourth - 1]:
                            fourth -= 1;
                        fourth -=1;
                second += 1;
            first += 1;
        return result;
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯