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等技术内容,立即学习