强密码检验器 II
模拟
class Solution {
public:
bool strongPasswordCheckerII(string s) {
string str="!@#$%^&*()-+";
bool a=false,b=false,c=false,d=false;
if(s.size()<8) return false;
for(int i=0;i<s.size()-1;i++)
{
if(s[i]==s[i+1]) return false;
}
for(int i=0;i<s.size();i++)
{
if(islower(s[i])) a=true;
else if(isupper(s[i])) b=true;
else if(isdigit(s[i])) c=true;
else if(str.find(to_string(s[i]))) d=true;
}
if(a&&b&&c&&d) return true;
return false;
}
};
咒语和药水的成功对数
排序+二分
class Solution {
public:
vector<int> successfulPairs(vector<int>& a, vector<int>& b, long long s) {
vector<int>res;
int cnt=0;
sort(b.begin(),b.end());
for(auto&x:a)
{
int cnt=b.end()-lower_bound(b.begin(),b.end(),(double)s/(double)x);
res.push_back(cnt);
}
return res;
}
};
替换字符后匹配
哈希表+模拟
class Solution {
public:
bool matchReplacement(string a, string b, vector<vector<char>>& mappings) {
unordered_map<char,set<char>>hash;
for(auto&p:mappings)
{
int x1=p[0];
int x2=p[1];
hash[x1].insert(x2);
}
int n=a.size();
int m=b.size();
for(int i=0;i+m-1<n;i++)
{
bool flag=true;
for(int j=0;j<m;j++)
{
if(a[i+j]!=b[j]&&!hash[b[j]].count(a[i+j]))
{
flag=false;
break;
}
}
if(flag) return true;
}
return false;
}
};
统计得分小于 K 的子数组数目
前缀和+双指针
class Solution {
public:
long long countSubarrays(vector<int>& nums, long long k) {
int n=nums.size();
vector<long long>s(n+1);
for(int i=1;i<=n;i++)
{
s[i]=nums[i-1]+s[i-1];
}
long long res=0;
for(int i=1,j=0;i<=nums.size();i++)
{
while((s[i]-s[j])*(i-j)>=k) j++;
res+=i-j;
}
return res;
}
};