严晏来的博客

  • 关于我
  • 我的吉他作品
  • 我热爱的辩论
  • 我的技术博客
  • 我的软件作品
  • 我的小记
你好哇!欢迎来到我的博客!
在代码世界里摸爬滚打的菜鸟程序员一枚,喜欢搭系统、写代码,喜欢追寻各种技术,热爱生活,喜欢唱歌、弹吉他。
  1. 首页
  2. 我的技术博客
  3. leetcode刷题记录
  4. 正文

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

2024年11月15日 511点热度 0人点赞 0条评论

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

454.四数相加II

给你四个整数数组 nums1、nums2、nums3 和 nums4 ,数组长度都是 n ,请你计算有多少个元组 (i, j, k, l) 能满足:

  • 0 <= i, j, k, l < n

  • nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0

这一题使用哈希表来做,使用unordereed_map,先遍历nums1、nums2,并存入map中,再遍历nums3、nums4,在map中寻找相反数。时间复杂度O(n2)。

class Solution {
public:
    int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {
        unordered_map<int,int> map;
        
        for(int a : nums1){
            for(int b : nums2){
                map[a+b] ++;
            }
        }
        int count = 0;
        for(int c:nums3){
            for(int d : nums4){
                if(map.find(0-(c+d)) != map.end()){
                    count += map[0-(c+d)];
                }
            }
        }
        return count;
    }
};

383.赎金信

和字母那道题很像,之前刷过了,这里直接贴上了。

class Solution {
public:
    bool canConstruct(string ransomNote, string magazine) {
        int record[26] = {0};
        for(int i = 0;i < magazine.length();i++){
            record[magazine[i] - 'a'] ++;
        }
        for(int j = 0;j < ransomNote.length();j++){
            record[ransomNote[j] - 'a'] --;
            if(record[ransomNote[j] - 'a'] < 0){
                return false;
            }
        }
        return true;
    }
};

15. 三数之和

细节太多了,还得刷。

class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        sort(nums.begin(),nums.end());
        vector<vector<int>> result;
        for(int i = 0 ; i < nums.size() ; i ++){
            if(nums[i] > 0){
                return result;
            }
            if(i > 0 && nums[i] == nums[i - 1]){
                continue;
            }
            int left = i + 1;
            int right = nums.size() -  1;
            while(right > left){
                if(nums[i] + nums[left] + nums[right] < 0){
                    left++;
                }
                else if(nums[i] + nums[left] + nums[right] > 0){
                    right--;
                }
                else{
                    result.push_back(vector<int>{nums[i] , nums[left] , nums[right]});
                    while(right > left && nums[right] == nums[right - 1]){
                        right--;
                    }
                    while(right > left && nums[left] == nums[left + 1]){
                        left++;
                    }
                    right--;
                    left++;
                }
            }
        }
        return result;
    }
};

18. 四数之和

和三数之和一样的解法,回头再来写吧。

标签: 暂无
最后更新:2024年11月15日

YanYanlai

这个人很懒,什么都没留下

点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

归档

  • 2024 年 11 月
  • 2024 年 10 月
  • 2024 年 8 月
  • 2024 年 5 月
  • 2023 年 6 月
  • 2023 年 3 月
  • 2022 年 12 月
  • 2022 年 9 月

分类

  • leetcode刷题记录
  • linux
  • web开发
  • 导航与定位
  • 我的小记
  • 我的技术博客
  • 我的软件作品
  • 数据结构与算法
  • 赵雷
  • 面试八股

COPYRIGHT © 2024 严晏来的博客. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang