当前位置:网站首页>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刷题——去除重复字母#316#Medium
- 微信支付宝账户体系和支付接口业务流程
- [leetcode] 577 reverse word III in string
- folium,确诊和密接轨迹上图
- Infra11199 database system
- [leetcode] 19 delete the penultimate node of the linked list
- JVM architecture, classloader, parental delegation mechanism
- 【LeetCode】486-预测赢家
- MD5 encryption
- [leetcode] 1020 number of enclaves
猜你喜欢

Download blender on Alibaba cloud image station

FPGA - clock-03-clock management module (CMT) of internal structure of 7 Series FPGA

2022 年辽宁省大学生数学建模A、B、C题(相关论文及模型程序代码网盘下载)

15_ Redis_ Redis. Conf detailed explanation

怎样从微信返回的json字符串中截取某个key的值?

19_ Redis_ Manually configure the host after downtime

JVM architecture, classloader, parental delegation mechanism

XML Configuration File

List set & UML diagram

Bing. Site Internet
随机推荐
【LeetCode】19-删除链表的倒数第N个结点
Steps for Navicat to create a new database
List set & UML diagram
Summary of the first three passes of sqli Labs
6.12 企业内部upp平台(Unified Process Platform)的关键一刻
20_ Redis_ Sentinel mode
Leetcode question brushing - parity linked list 328 medium
Wechat Alipay account system and payment interface business process
LeetCode刷题——两整数之和#371#Medium
YOLOV5 代码复现以及搭载服务器运行
Common English abbreviations for data analysis (I)
高考录取分数线爬取
. Solution to the problem of Chinese garbled code when net core reads files
搭建自己的语义分割平台deeplabV3+
Guangzhou Emergency Management Bureau issued a high temperature and high humidity chemical safety reminder in July
终于搞懂了JS中的事件循环,同步/异步,微任务/宏任务,运行机制(附笔试题)
08_ strand
【LeetCode】977-有序數組的平方
Engineer evaluation | rk3568 development board hands-on test
Case introduction and problem analysis of microservice