LeetCode 3、28、1351

3、无重复字符的最长子串

题目:
在这里插入图片描述
代码:

//双指针
class Solution {
public:
    int lengthOfLongestSubstring(string s) {

        unordered_map<char,int> hash;
        int res = 0;
        for (int i = 0, j = 0; j < s.size(); j ++) {
            hash[s[j]] ++;
            while (hash[s[j]] > 1) hash[s[i ++]] --;
            res = max(res, j - i + 1);  //维护最大的子串
        }

        return res;
    }
};

28、Implement strStr()

题目:
在这里插入图片描述
代码:

//暴力枚举
class Solution {
public:
    int strStr(string haystack, string needle) {
        int n = haystack.length(), m = needle.length();
        for (int i = 0; i < n - m + 1; i++) {
            bool is_same = true;
            for (int j = 0; j < m; j++)
                if (haystack[i + j] != needle[j]) {
                    is_same = false;
                    break;
                }
            if (is_same )
                return i;
        }
        return -1;
    }
};

1351

题目:
在这里插入图片描述
代码:

class Solution {
public:
    int countNegatives(vector<vector<int>>& grid) {
        int num=0;
        for (auto x:grid){
            int l=0,r=(int)x.size()-1,pos=-1;
            while (l<=r){
                int mid=l+((r-l)>>1);
                if (x[mid]<0){
                    pos=mid;
                    r=mid-1;
                }
                else l=mid+1;
            }
            if (~pos) num+=(int)x.size()-pos;// pos=-1表示这一行全是>=0的数,不能统计
        }
        return num;
    }
};

补充

LeetCode 85最大矩形

充电站
推荐一个零声学院免费公开课程,个人觉得老师讲得不错,分享给大家:Linux,Nginx,ZeroMQ,MySQL,Redis,fastdfs,MongoDB,ZK,流媒体,CDN,P2P,K8S,Docker,TCP/IP,协程,DPDK等技术内容,立即学习

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值