当前位置:网站首页>六月集训 第二天——字符串
六月集训 第二天——字符串
2022-06-12 06:27:00 【误入佬群】
文章目录
前言
今天算法的内容是:字符串
一、字母在字符串中的百分比
给你一个字符串 s 和一个字符 letter ,返回在 s 中等于 letter 字符所占的 百分比 ,向下取整到最接近的百分比。
一、思路:
(1)符合条件进行记录,最后进行百分比计算
二、源码
class Solution {
public:
int percentageLetter(string s, char letter) {
double count=0;
for(int i=0;i<s.size();i++){
if(s[i]==letter){
++count;
}
}
int x=count/s.size()*100;
//printf("%d",s.size());
return x;
}
};
三.知识点
线性枚举
二、 学生出勤记录 I
给你一个字符串 s 表示一个学生的出勤记录,其中的每个字符用来标记当天的出勤情况(缺勤、迟到、到场)。记录中只含下面三种字符:
‘A’:Absent,缺勤
‘L’:Late,迟到
‘P’:Present,到场
如果学生能够 同时 满足下面两个条件,则可以获得出勤奖励:
按 总出勤 计,学生缺勤(‘A’)严格 少于两天。
学生 不会 存在 连续 3 天或 连续 3 天以上的迟到(‘L’)记录。
如果学生可以获得出勤奖励,返回 true ;否则,返回 false 。
一、思路:
(1)对于缺勤进行一个记录
(2)对于迟到进行一个连续天数的记录,保留最高的记录,时刻与最高记录进行对比保持更新,断开则记录归零
二、源码
class Solution {
public:
bool checkRecord(string s) {
int count=0,n=0;
int max=0;
for(int i=0;i<s.size();i++){
if(s[i]=='A'){
count++;
}
//就正常的 进行 一个对比记录即可
if(s[i]=='L'){
n++;
max=max>n?max:n;
}else{
n=0;
}
}
if(count<2&&max<3){
return true;
}
return false;
}
};
三.知识点
线性枚举
三、统计是给定字符串前缀的字符串数目
给你一个字符串数组 words 和一个字符串 s ,其中 words[i] 和 s 只包含 小写英文字母 。
请你返回 words 中是字符串 s 前缀 的 字符串数目 。
一个字符串的 前缀 是出现在字符串开头的子字符串。子字符串 是一个字符串中的连续一段字符序列。(不懂看例子)
示例 1:
输入:words = [“a”,“b”,“c”,“ab”,“bc”,“abc”], s = “abc”
输出:3
解释:
words 中是 s = “abc” 前缀的字符串为:
“a” ,“ab” 和 “abc” 。
所以 words 中是字符串 s 前缀的字符串数目为 3 。
示例 2:
输入:words = [“a”,“a”], s = “aa”
输出:2
解释:
两个字符串都是 s 的前缀。
注意,相同的字符串可能在 words 中出现多次,它们应该被计数多次。
一、思路:
(1)循环字符串数组行数,对每行的字符串列数进行线性判断是否属于字符串s中子字符串并,进行一个计数
(2)实现一个列数判断是否属于字符串s的子字符,
为:
字符当行长度大于字符串s的长度,错
符合,然后进行对该行字符串进行线性遍历,判断是否相符和,没问题就对辣
二、源码
class Solution {
bool is(string& pre,string& s){
if(pre.size()>s.size()){
return false;
}
for(int i=0;i<pre.size();i++){
if(pre[i]!=s[i]){
return false;
}
}
return true;
}
public:
int countPrefixes(vector<string>& words, string s) {
int count=0;
for(int i=0;i<words.size();i++){
if(is(words[i],s)){
count++;
}
}
return count;
}
};
三.知识点
构造函数如何进行传入数组中参数? 对数组进行引用符号&
bool is(string& pre,string& s)
边栏推荐
- Solution: content type 'application/x-www-form-urlencoded; charset=UTF-8‘ not supported
- Sensor bringup 中的一些问题总结
- Explanation of sensor flicker/banding phenomenon
- Single channel picture reading
- SQL注入原理即sqli-labs搭建,sql注入简单实战
- Summary of some problems in sensor bring up
- JS variable scope
- Excel VBA opens a file that begins with the specified character
- Multithreading (4) -- no lock (2) -- Atomic related atomic classes
- Cv2.fillpoly coco annotator segment coordinate conversion to mask image
猜你喜欢

UE4 4.27 modify the mobile forward pipeline to support cluster multi light source culling

MNIST handwritten data recognition by CNN

SQL 注入-盲注

SQL 注入读写文件

The vs 2019 community version Microsoft account cannot be logged in and activated offline

Cause analysis of motion blur / smear caused by camera shooting moving objects

Information content security experiment of Harbin Institute of Technology

(UE4 4.27) customize primitivecomponent

Word vector training based on nnlm

相机图像质量概述
随机推荐
MLP sensor
SQL 注入读写文件
Multithreading Foundation (XI) -- prevent CPU from occupying 100%
Unity3d multi platform method for reading text files in streamingasset directory
集合判断存在交集
LeetCode-219. Duplicate Element II present
张驰课堂:2022年CAQ中质协六西格玛考试时间通知
LeetCode-997. Find the town judge
SQL injection - Union query
Use ms17-010 Eternal Blue vulnerability to infiltrate win7 and establish a permanent back door
Storing texture2d to hard disk JPG file with script under unity3d
Codeforces Round #793 (Div. 2) A B C
LeetCode-2034. Stock price fluctuation
Chartextcnn (Ag dataset - news topic classification)
MNIST handwritten data recognition by RNN
Opencv_ 100 questions_ Chapter V (21-25)
PHP 读写 COOKIE
Multithreading mode (I) -- protective pause and join source code
Zip and Items() difference
AI operation ch8