20210718LeedCode第 250 场周赛(六)

20210718LeedCode第 250 场周赛(六)

 stringstream字符串流的学习:

 1 string s;
 2 stringstream ss;
 3 int n, i, sum, a;
 4 cin >> n;
 5 getline(cin, s); // 整数后面换行,需要吃掉换行符
 6 for (i=0; i<n; i++)
 7 {
 8     getline(cin, s) // 字符串后面换行,换行符自动转为空字符 ,不需要吃掉换行符。
 9      ss.clear();
10     ss.str(s);
11     sum=0;
12     while (1)
13     {
14         ss >> a;
15         if ( ss.fail() ) break;
16         sum+=a;
17     }
18     cout << sum << endl;
19 }

参考博客:https://blog.csdn.net/xw20084898/article/details/21939811

5161. 可以输入的最大单词数

题目:

键盘出现了一些故障,有些字母键无法正常工作。而键盘上所有其他键都能够正常工作。

给你一个由若干单词组成的字符串 text ,单词间由单个空格组成(不含前导和尾随空格);另有一个字符串 brokenLetters ,由所有已损坏的不同字母键组成,返回你可以使用此键盘完全输入的 text 中单词的数目。

 题解:

主要是stringstream和map的结合。

代码:

 1 class Solution {
 2 public:
 3     int canBeTypedWords(string text, string brokenLetters) {
 4         //先把损坏的字符用map统计出来
 5         map<char,int> mp;
 6         for(const auto & i : brokenLetters)
 7         {
 8             mp[i]++;
 9         }
10         int ans=0;
11         //定义字符串流和字符串输出为一个个的字符串
12         string s;
13         stringstream ss;
14         ss.clear();//ss先进行清空操作
15         ss.str(text);//把text内容写入ss中
16         while(ss>>s)//循环ss,每次输出一个字符串到s中
17         {
18             int flag=1;
19             for(const auto & i : s)
20             {
21                 if(mp[i]==1) //判断每个字符串s中是否有损坏的字符,可以循环每个字符串s
22                 {
23                     flag=0;
24                 }
25             }
26             ans+=flag;
27         }
28         return ans;
29     }
30 };

5814. 新增的最少台阶数

 题目:

给你一个 严格递增 的整数数组 rungs ,用于表示梯子上每一台阶的 高度 。当前你正站在高度为 0 的地板上,并打算爬到最后一个台阶。

另给你一个整数 dist 。每次移动中,你可以到达下一个距离你当前位置(地板或台阶)不超过 dist 高度的台阶。当然,你也可以在任何正 整数 高度处插入尚不存在的新台阶。

返回爬到最后一阶时必须添加到梯子上的 最少 台阶数。

 题解:

遍历rungs数组,rungs.size()。只要两个阶梯之间的差值大于dist步长,就会增加阶梯。增加阶梯的方法是:如果差值可以整除dist,就是差值除以dist-1;如果不可以整除,就是差值除以dist。需要注意的是:最初的阶梯为0,所以对0和第一步阶梯的值也要判断。

代码:

 1 class Solution {
 2 public:
 3     int addRungs(vector<int>& rungs, int dist) {
 4         //rungs是梯子上所以有的阶梯的位置,dist是人的步长
 5         int ans=0;
 6         for(int i=1;i<rungs.size();i++)
 7         {
 8             if(rungs[i]-rungs[i-1]<=dist)
 9             {
10                 continue;
11             }
12             else
13             {
14                 if((rungs[i]-rungs[i-1])%dist==0)
15                 {
16                     //cout<<rungs[i]<<endl;
17                     ans+=(rungs[i]-rungs[i-1])/dist-1;
18                 }
19                 else
20                 {
21                     ans+=(rungs[i]-rungs[i-1])/dist;
22                 }
23 
24             }
25         }
26         if(rungs[0]>dist)
27         {
28             if(rungs[0]%dist==0)
29             {
30                 ans+=rungs[0]/dist-1;
31             }
32             else
33             {
34                 ans+=rungs[0]/dist;
35             }
36         }
37         return ans;
38     }
39 };

还有用动态规划DP也可以做。

参考链接:https://leetcode-cn.com/problems/maximum-number-of-words-you-can-type

https://leetcode-cn.com/problems/maximum-number-of-words-you-can-type/solution/c-ha-xi-biao-stringstream-by-ycynbnb-gzqn/

 https://leetcode-cn.com/problems/add-minimum-number-of-rungs

雪儿言
原文地址:https://www.cnblogs.com/weixq351/p/15026272.html