代码随想录算法训练营第七天 | 哈希表part02 | 454.四数相加II 383. 赎金信 15. 三数之和 18. 四数之和

454.四数相加II 

解析: 代码随想录

思路

暴力遍历,n^4的时间复杂度

可以先遍历其中一个或两个或三个数组,记录遍历过程中出现的元素和及其出现的次数,

再遍历剩余的数组,判断过程中出现的元素和是否能和已记录的元素和相加为0,若为0,将该已记录元素和的出现次数加到结果。

先遍历两个数组再遍历两个数组的时间复杂度为O(2*n^2), 先遍历一或三个数组的时间复杂度为O(N^3)。

实现

 四个数组的元素范围太大,不能用数组记录

  • -2^28 <= nums1[i], nums2[i], nums3[i], nums4[i] <= 2^28

使用unordered_map

题解

class Solution {
public:
    int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {
        unordered_map<int, int> ABset;
        int Ret = 0;
        for(int num1 : nums1)
        {
            for(int num2 : nums2)
            {
                ABset[num1 + num2]++;
            }
        }
        for(int num3 : nums3)
        {
            for(int num4 : nums4)
            {
                if(ABset.find(-(num3 + num4)) != ABset.end())
                {
                    Ret += ABset[-(num3 + num4)];
                }
            }
        }
        return Ret;
    }
};

 383. 赎金信

题目来源:leetcode 题库 383. 赎金信

思路

题目给定两个字符串,判断第一个字符串能不能由第二个里面的字符构成。

题目字符串只包含26个小写字母,创建长度为26的统计数组。

小写字母字符['a' , 'z'], 减去'a'后能得到[0,25]的范围。

字符串以'\0'为结束符。

遍历第一个字符串,在统计数组中统计每个小写字母字符存在的数量,

遍历第二个字符串,在统计数组中减去每个小写字母字符存在的数量,

遍历统计数组,若存在大于0的元素,说明第一个字符串里的该元素的数量大于第二个字符串里的该元素的数量,则第一个字符串能不能由第二个里面的字符构成。

题解 

bool canConstruct(char * ransomNote, char * magazine){
    int i = 0;
    int counters[26] = {0};
    bool Ret = true;
    while(*ransomNote != '\0')
    {
        counters[*ransomNote - 'a']++;
        ransomNote++;
    }
    while(*magazine != '\0')
    {
        counters[*magazine - 'a']--;
        magazine++;
    }
    for(i = 0; i < 26; i++)
    {
        if(counters[i] > 0)
        {
            Ret = false;
            break;
        }
    }
    return Ret;
}

15. 三数之和

题目来源:leetcode 题库 15. 三数之和

解析:代码随想录

思路

由于要返回所有满足三数之和为零的所有元素(不能重复,不同顺序但元素相同视为重复)

只求元素,不求索引,可将初始数组排序,相同的元素被排在了相邻的位置,利于去重

第一个索引遍历数组,其值作为第一个值;第二、三个值的索引从第一个索引之后取

  • 由于数组已排序,若当前索引的值已大于零,三数之和不再可能大于零,break退出循环
  • 判断当前索引的值不与上一索引的值相同,continue舍弃此分支,可去重
  • 接着想办法找到能让第二、三个值与第一个索引的值相加等于零的所有索引组合,由于数组已排序,可让第二个索引从第一个索引+1出发,第三个索引从数组尾出发:
    • 当第二、三个索引不重叠时
      • 若三数相加等于0,将当前三个索引的值记录结果
        • 当第二个索引的加一后的索引的值与第二个索引的值相等时,第二个索引加一,直至第二个索引的值与其加一后的索引的值不相等,第二个索引移至值不相等的加一后的索引
        • 当第三个索引的减一后的索引的值与第三个索引的值相等时,第三个索引减一,直至第三个索引的值与其减一后的索引的值不相等,第三个索引移至值不相等的减一后的索引
      • 若三数相加小于0,第二个索引加一,试使三数之和变大
      • 若三数相加大于0,第三个索引减一,试使三数之和变小

题解 

class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        vector<vector<int>> Ret;
        sort(nums.begin(), nums.end());
        int first, left, right, target;
        for(first = 0; first < nums.size(); first++)
        {
            if(nums[first] > 0)
            {
                break;
            }

            if((first >0) && (nums[first] == nums[first - 1]))
            {
                continue;
            }

            target = -nums[first];
            left = first + 1;
            right = nums.size() - 1;

            while(left < right){
                if(nums[left] + nums[right] == target )
                {
                    Ret.push_back({nums[first], nums[left], nums[right]});
                    while((left < right) && (nums[left] == nums[left + 1]))
                    {
                        left++;
                    }
                    while((left < right) && (nums[right] == nums[right - 1]))
                    {
                        right--;
                    }
                    left ++;
                    right --;
                }
                else if(nums[left] + nums[right] > target)
                {
                    right --;
                }
                else
                {
                    left++;
                }
            }
        }
        return Ret;
    }
};

18. 四数之和

题目来源:leetcode 题库 18. 四数之和

解析:代码随想录

 思路

大致思路同15. 三数之和

不同的是,目标值可正可负,不一定是0了,此时剪枝时要注意保证当前索引的值大于0,否则会出现加上后续的索引的值后比目标值小的情况,如nums = [-3, -1, 0, 0], target = -4

/* 第一个索引的剪枝 */

if((nums[first] > target) && (nums[first] >= 0))

{

    break;

}

/* 第二个索引的剪枝 */

if((nums[first] + nums[second] > target) && (nums[second] >= 0))

{

    break;

}

在第一个索引的遍历条件中,由于要在第一个索引之后还要取另外三个值,所以遍历结束条件可以写为小于长度减3,第二个索引的遍历条件同理。也算一个小剪枝

for(first = 0; first < n - 3; first++)

{

    for(second = first + 1; second < n -2 ; second++)

    {

由于数组元素的取值范围是

  • -10^9 <= nums[i] <= 10^9

四个元素相加的范围是[ - 4*10^9, 4*10^9],int类型的取值范围是[-2147483648,2147483647],可能会导致溢出,使用long重定义计算结果类型使其不溢出

 题解

class Solution {
public:
    vector<vector<int>> fourSum(vector<int>& nums, int target) {
        vector<vector<int>> Ret;
        sort(nums.begin(), nums.end());
        int first, second, left, right;
        int n = nums.size();
        for(first = 0; first < n - 3; first++)
        {
            if((nums[first] > target) && (nums[first] >= 0))
            {
                break;
            }

            if((first > 0) && (nums[first] == nums[first - 1]))
            {
                continue;
            }

            for(second = first + 1; second < n -2 ; second++)
            {
                if((nums[first] + nums[second] > target) && (nums[second] >= 0))
                {
                    break;
                }

                if((second > first + 1) && (nums[second] == nums[second - 1]))
                {
                    continue;
                }

                left = second + 1;
                right = n - 1;
                while(left < right)
                {
                    if(((long)nums[first] + nums[second] + nums[right] + nums[left]) == target)
                    {
                        Ret.push_back({nums[first], nums[second], nums[left], nums[right]});
                        while((left < right) && (nums[left] == nums[left + 1]))
                        {
                            left++;
                        }
                        while((left < right) && (nums[right] == nums[right - 1]))
                        {
                            right--;
                        }
                        left ++;
                        right --;
                    }
                    else if(((long)nums[first] + nums[second] + nums[right] + nums[left]) > target)
                    {
                        right --;
                    }
                    else
                    {
                        left++;
                    }
                }
            }
        }
        return Ret;
    }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值