力扣每日一题
58. 最后一个单词的长度
给你一个字符串 s,由若干单词组成,单词前后用一些空格字符隔开。返回字符串中最后一个单词的长度。
单词 是指仅由字母组成、不包含任何空格字符的最大子字符串。
示例 1:
输入:s = "Hello World"
输出:5
创建一个整型变量num计算最后一个单词的长度,把字符串从最后一个索引往前遍历,遇到字母则num++,一旦遇到空格就结束遍历,输出num。因为结尾第一个字符也可能是空格,所以加一个判断,遇到空格是判断num是否为0,如果不为0就输出num,如果为0跳过这个空格遍历下一个字符。
class Solution {
public:
int lengthOfLastWord(string s) {
int num=0;
for(int i=s.size()-1;i>=0;i--){
if(s[i]==' '){
if(num==0){
continue;
}else{
return num;
}
}else{
num++;
}
}
return num;
}
};
洛谷
P1319 压缩技术
题目描述
设某汉字由N × N的0和1的点阵图案组成。
我们依照以下规则生成压缩码。连续一组数值:从汉字点阵图案的第一行第一个符号开始计算,按书写顺序从左到右,由上至下。第一个数表示连续有几个0,第二个数表示接下来连续有几个1,第三个数再接下来连续有几个0,第四个数接着连续几个1,以此类推……
例如: 以下汉字点阵图案:
0001000
0001000
0001111
0001000
0001000
0001000
1111111
对应的压缩码是: 7 3 1 6 1 6 4 3 1 6 1 6 1 3 7 (第一个数是N ,其余各位表示交替表示0和1 的个数,压缩码保证 N × N=交替的各位数之和)
既然输出结果这么花,那我们就用二维数组来实现,创建一个vector<int>v来存放所有的1和0,创建一个vector<vector<int>>chinese来存放v。还创建了一个vector<int>math来存放压缩密码。写一个while循环接收压缩密码,当压缩密码总和等于n*n结束接收,用一个for循环遍历math,里面嵌套一个while来往v里输入1或0,既然一开始是从0开始的,那么加一个判断,如果math[i]中的i是偶数,输入0,若是奇数输入1.每插入一个数对v进行判断,若v.size()==n,将v存入chinese,同时清空v。最后对chinese遍历输出
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<list>
#include<string>
#include<map>
#include <set>
#include <iomanip>
int main()
{
int n,l=0,num,sum=0,score;
cin >> n;
vector<vector<int>>chinese;
vector<int>v;
vector<int>math;
while (sum!=n*n)
{
cin >> num;
sum += num;
math.push_back(num);
}
l = n;
while (l--)
{
for (int i = 0; i < math.size(); i++)
{
score = math[i];
if (i % 2 == 0)
{
while (score--)
{
v.push_back(0);
if (v.size() == n)
{
chinese.push_back(v);
v.clear();
}
}
}
else
{
while (score--)
{
v.push_back(1);
if (v.size() == n)
{
chinese.push_back(v);
v.clear();
}
}
}
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < chinese[i].size(); j++)
{
cout << chinese[i][j];
}
cout << endl;
}
return 0;
}
P1320 压缩技术(续集版)
题目描述
设某汉字由N × N的0和1的点阵图案组成。
我们依照以下规则生成压缩码。连续一组数值:从汉字点阵图案的第一行第一个符号开始计算,按书写顺序从左到右,由上至下。第一个数表示连续有几个0,第二个数表示接下来连续有几个1,第三个数再接下来连续有几个0,第四个数接着连续几个1,以此类推……
例如: 以下汉字点阵图案:
0001000
0001000
0001111
0001000
0001000
0001000
1111111
对应的压缩码是: 7 3 1 6 1 6 4 3 1 6 1 6 1 3 7 (第一个数是N ,其余各位表示交替表示0和1 的个数,压缩码保证 N×N = 交替的各位数之和)
输入格式
汉字点阵图(点阵符号之间不留空格)。(3<=N<=200)
输出格式
一行,压缩码。
(好家伙还有续集)条件和上一题一样,只不过是倒过来,上一题是给压缩密码输出文字,这题是给文字输出压缩密码。这里我用来接收文字的是字符串不是数组,没啥好说的,上一题会了这一题自然也不难。注意就是第一个压缩密码必须是0的,如果文字是从1开始的,那就要先在存储密码的容器前先插入一个数字0,代表一开始没有‘0’。
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<list>
#include<string>
#include<map>
#include <set>
#include <iomanip>
int main()
{
int num1=0,num0=0, n, l = 1,sum=0;
string str;
vector<int>math;
do
{
cin >> str;
n = str.size();
sum += str.size();
for (int i = 0; i < str.size(); i++)
{
if (str[i] == '1')
{
if (num0 != 0)
{
math.push_back(num0);
num0 = 0;
}
num1++;
}
else
{
if (num1 != 0)
{
if (math.size() == 0)
{
math.push_back(0);
}
math.push_back(num1);
num1 = 0;
}
num0++;
}
}
str.clear();
} while (sum != n * n);
if (num1 != 0)
{
if (math.size() == 0)
{
math.push_back(0);
}
math.push_back(num1);
}
else if (num0 != 0)
{
math.push_back(num0);
}
cout << n << " ";
for (int i = 0; i < math.size(); i++)
{
cout << math[i] << " ";
}
return 0;
}