当前位置:网站首页>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);
}
};
边栏推荐
- Party History Documentary theme public welfare digital cultural and creative products officially launched
- Force deduction solution summarizes the lucky numbers in 1380 matrix
- [leetcode] 977 - carré du tableau ordonné
- Leetcode skimming - remove duplicate letters 316 medium
- Engineer evaluation | rk3568 development board hands-on test
- 基于RZ/G2L | OK-G2LD-C开发板存储读写速度与网络实测
- Yolo format data set processing (XML to txt)
- 18_ Redis_ Redis master-slave replication & cluster building
- 工程师评测 | RK3568开发板上手测试
- 搭载TI AM62x处理器,飞凌FET6254-C核心板首发上市!
猜你喜欢
List set & UML diagram
损失函数与正负样本分配:YOLO系列
PTA ladder game exercise set l2-001 inter city emergency rescue
工程师评测 | RK3568开发板上手测试
彻底弄懂浏览器强缓存和协商缓存
I made an istio workshop. This is the first introduction
Redux - detailed explanation
17_ Redis_ Redis publish subscription
MySQL -- Index Optimization -- order by
How to intercept the value of a key from the JSON string returned by wechat?
随机推荐
. Solution to the problem of Chinese garbled code when net core reads files
LeetCode刷题——递增的三元子序列#334#Medium
LeetCode刷题——验证二叉树的前序序列化#331#Medium
PTA ladder game exercise set l2-001 inter city emergency rescue
16_ Redis_ Redis persistence
There are 7 seats with great variety, Wuling Jiachen has outstanding product power, large humanized space, and the key price is really fragrant
【LeetCode】486-预测赢家
FPGA - clock-03-clock management module (CMT) of internal structure of 7 Series FPGA
Redux - detailed explanation
FPGA - 7系列 FPGA内部结构之Clocking -03- 时钟管理模块(CMT)
Bing.com网站
How to intercept the value of a key from the JSON string returned by wechat?
Leetcode skimming - remove duplicate letters 316 medium
MD5 encryption
语义分割学习笔记(一)
How to choose a third-party software testing organization for automated acceptance testing of mobile applications
02.面向容器化后,必须面对golang
Huffman tree: (1) input each character and its weight (2) construct Huffman tree (3) carry out Huffman coding (4) find hc[i], and get the Huffman coding of each character
[leetcode] 977 square of ordered array
【LeetCode】876-链表的中间结点