力扣题:哈希表与统计-10.28

力扣题-10.28

[力扣刷题攻略] Re:从零开始的力扣刷题生活

力扣题1:554. 砖墙

解题思想:穿过的砖头数和穿过边缘的砖头数相加为墙的高度,统计穿过边缘的砖头数即可

在这里插入图片描述

class Solution(object):
    def leastBricks(self, wall):
        """
        :type wall: List[List[int]]
        :rtype: int
        """
        cnt = {}
        for i in range(len(wall)):
            a = 0
            for j in range(len(wall[i])-1):
                a = a + wall[i][j]
                cnt[a] = cnt.get(a, 0) + 1
        max_num =0
        for key, val in cnt.items():
            max_num = max(max_num,val)
        return len(wall)-max_num
class Solution {
public:
    int leastBricks(vector<vector<int>>& wall) {
        unordered_map<int, int> cnt;
        for(int i=0;i<wall.size();i++){
            int a = 0;
            for(int j=0;j<wall[i].size()-1;j++){
                a = a+wall[i][j];
                cnt[a] = cnt[a] + 1;
            }
        }
        int max_num=0;
        for (const auto& pair : cnt) {
            max_num = std::max(max_num, pair.second);
        }
        return wall.size()-max_num;
    }
};

力扣题2:609. 在系统中查找重复文件

解题思想:通过字符对字符串进行分解,然后通过哈希表进行存储,最后输出有两个路径以上的答案

在这里插入图片描述

class Solution(object):
    def findDuplicate(self, paths):
        """
        :type paths: List[str]
        :rtype: List[List[str]]
        """
        aux = {}
        for i in range(len(paths)):
            values = paths[i].split(" ")
            path = values[0]
            for j in range(1,len(values)):
                file_name = values[j].split("(")
                content = file_name[1].replace(")","")
                file_path = path+'/'+file_name[0]
                if content in aux:
                    aux[content].append(file_path)
                else:
                    aux[content] = [file_path]
        print(aux)
        result = []
        for key,value in aux.items():
            if len(value)>=2:
                result.append(value)
        return result
class Solution {
public:
    vector<vector<string>> findDuplicate(vector<string>& paths) {
        std::unordered_map<std::string, std::vector<std::string>> aux;
        for(int i=0;i<paths.size();i++){
            //实现values = paths[i].split(" ")
            std::vector<std::string> values;
            std::stringstream ss(paths[i]);
            std::string token;
            while (ss >> token) {
                values.push_back(token);
            }
            string path = values[0];
            for(int j=1;j<values.size();j++){
                size_t pos = values[j].find("(");
                std::string file_name;
                std::string content;
                if (pos != std::string::npos) {
                    file_name = values[j].substr(0, pos);
                }
                std::string temp = values[j].substr(pos+1, values[j].size());
                pos = temp.find(")");
                if (pos != std::string::npos) {
                    content = temp.substr(0, pos);
                }
                std::string file_path = path+'/'+file_name;
                aux[content].push_back(file_path);
            }
        }

        std::vector<std::vector<std::string>> result;
        for (const auto& entry : aux) {
            if (entry.second.size() >= 2) {
                result.push_back(entry.second);
            }
        }
        return result;
    }
};

力扣题3:454. 四数相加 II

解题思想:进行统计相加即可

在这里插入图片描述

class Solution(object):
    def fourSumCount(self, nums1, nums2, nums3, nums4):
        """
        :type nums1: List[int]
        :type nums2: List[int]
        :type nums3: List[int]
        :type nums4: List[int]
        :rtype: int
        """
        ans = 0
        count12 = collections.Counter(u + v for u in nums1 for v in nums2)
        count34 = collections.Counter(u + v for u in nums3 for v in nums4)
        for key,value in count12.items():
            if -key in count34:
                ans = ans+value*count34[-key]
        return ans
class Solution {
public:
    int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {
        int ans=0;
        unordered_map<int, int> count12;
        unordered_map<int, int> count34;

        for (int u: nums1) {
            for (int v: nums2) {
                count12[u + v]++;
            }
        }
        for (int u: nums3) {
            for (int v: nums4) {
                count34[u + v]++;
            }
        }
        for(auto [key, value] : count12){
            if(count34[-key]>0){
                ans = ans+value*count34[-key];
            }
        }
        return ans;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值