leetcode[76. 最小覆盖子串] 引发的关于变量出栈入栈的思考

原题

给你一个字符串 s 、一个字符串 t 。返回 s 中涵盖 t 所有字符的最小子串。如果 s 中不存在涵盖 t 所有字符的子串,则返回空字符串 "" 。

注意:如果 s 中存在这样的子串,我们保证它是唯一的答案。

刚看到这题第一反应是使用滑动窗口+hashmap内部判断滑动窗口是否满足条件。于是码完代码测试后提交,发现超时。代码如下:

class Solution {
public:
    bool check(unordered_map<char, int> org, unordered_map<char, int> cnt)
    {
        for (auto it : org) {
            if (it.second > cnt[it.first]) return false;
        }
        return true;
    }
    
    string minWindow(string s, string t) {
        int len1 = t.size();
        int len2 = s.size();
        int left = 0, right = 0;
        int retLeft = 0, retRight = 0;
        string ret;
        unordered_map<char, int> org, cnt;

        while (right < len1) ++org[t[right++]];
        right = 0;
        while (right < len2) {
            ++cnt[s[right++]];
            bool checked = false;
            while (check(org, cnt)) {
                checked = true;
                --cnt[s[left++]];
            }
           
            if (checked && (ret.size() == 0 || right - left + 1 < ret.size())) ret = s.substr(left - 1, right - left + 1);
            if (ret.size() == len1) return ret;
        }
        
        return ret;
    }
};

后翻越官方题解,发现和我的差不多,不过官方题解的 map 是放在全局变量的,没有跟随函数入参。试了一下将我代码中的map声明为全局变量,果然能通过了。代码如下:

class Solution {
public:
    unordered_map<char, int> org, cnt;
    
    bool check()
    {
        for (auto it : org) {
            if (it.second > cnt[it.first]) return false;
        }
        return true;
    }
    
    string minWindow(string s, string t) {
        int len1 = t.size();
        int len2 = s.size();
        int left = 0, right = 0;
        int retLeft = 0, retRight = 0;
        string ret;
        

        while (right < len1) ++org[t[right++]];
        right = 0;
        while (right < len2) {
            ++cnt[s[right++]];
            bool checked = false;
            while (check()) {
                checked = true;
                --cnt[s[left++]];
            }

            if (checked && (ret.size() == 0 || right - left + 1 < ret.size())) ret = s.substr(left - 1, right - left + 1);
            if (ret.size() == len1) return ret;
        }
        
        return ret;
    }
};

于是又试了一下函数传参时使用引用传递,也能通过,代码如下:

class Solution {
public:
    bool check(unordered_map<char, int> &org, unordered_map<char, int> &cnt)
    {
        for (auto it : org) {
            if (it.second > cnt[it.first]) return false;
        }
        return true;
    }
    
    string minWindow(string s, string t) {
        int len1 = t.size();
        int len2 = s.size();
        int left = 0, right = 0;
        int retLeft = 0, retRight = 0;
        string ret;
        unordered_map<char, int> org, cnt;

        while (right < len1) ++org[t[right++]];
        right = 0;
        while (right < len2) {
            ++cnt[s[right++]];
            bool checked = false;
            while (check(org, cnt)) {
                checked = true;
                --cnt[s[left++]];
            }

            if (checked && (ret.size() == 0 || right - left + 1 < ret.size())) ret = s.substr(left - 1, right - left + 1);
            if (ret.size() == len1) return ret;
        }
        
        return ret;
    }
};

总结:

函数传参时尽量不要使用占用较大内存的参数,参数占用栈内存过大,不仅出栈入栈时性能消耗较多,而且还有可能造成栈溢出,应尽量使用引用或指针。

这都是我司编码规范的要求,还是要熟记并且运用起来啊!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值