当前位置:网站首页>2278. 字母在字符串中的百分比
2278. 字母在字符串中的百分比
2022-07-02 12:10:00 【紫菜(Nori)】
问题:
为什么,当代码中的times变量为float类型是,执行case会得到错误的结果?
class Solution {
public:
int percentageLetter(string s, char letter) {
// 这里使用浮点数,方便进行浮点数的除法运算
/* Note:
如果这里使用float类型将结果装换为int时会导致结果减一
case: times = 59; size = 100,返回58
*/
// 字母出现次数
double times = 0;
for(int i = 0; i < s.size(); i++){
if(s[i] == letter){
times++;
}
}
// 将结果扩大100被再去除小数部分,这里要求向下取证
return (int)((times/s.size())*100);
}
};
边栏推荐
- [leetcode] 19 delete the penultimate node of the linked list
- Equipped with Ti am62x processor, Feiling fet6254-c core board is launched!
- 自定义异常
- 6092. 替换数组中的元素
- Pytoch saves tensor to Mat file
- 高考分数线爬取
- 03.golang初步使用
- 10_ Redis_ geospatial_ command
- How to choose a third-party software testing organization for automated acceptance testing of mobile applications
- Solve the problem of frequent interruption of mobaxterm remote connection
猜你喜欢
LeetCode刷题——两整数之和#371#Medium
Let your HMI have more advantages. Fet-g2ld-c core board is a good choice
语义分割学习笔记(一)
Bing.com网站
03. Preliminary use of golang
自定义异常
2022 college students in Liaoning Province mathematical modeling a, B, C questions (related papers and model program code online disk download)
Pytoch saves tensor to Mat file
工程师评测 | RK3568开发板上手测试
LeetCode刷题——去除重复字母#316#Medium
随机推荐
PTA ladder game exercise set l2-001 inter city emergency rescue
QML pop-up frame, customizable
[leetcode] 1140 stone game II
【Leetcode】167-两数之和II -输入有序数组
Yolo format data set processing (XML to txt)
【LeetCode】344-反转字符串
飞凌嵌入式RZ/G2L处理器核心板及开发板上手评测
List set & UML diagram
NBA player analysis
. Net again! Happy 20th birthday
[leetcode] 189 rotation array
LeetCode_ Sliding window_ Medium_ 395. Longest substring with at least k repeated characters
11_ Redis_ Hyperloglog_ command
Solution of Queen n problem
Let your HMI have more advantages. Fet-g2ld-c core board is a good choice
04.进入云原生后的企业级应用构建的一些思考
08_ strand
Party History Documentary theme public welfare digital cultural and creative products officially launched
怎样从微信返回的json字符串中截取某个key的值?
12_Redis_Bitmap_命令