题目描述
句子 是由若干个单词组成的字符串,单词之间用单个空格分隔,其中每个单词可以包含数字、小写字母、和美元符号 '$'
。如果单词的形式为美元符号后跟着一个非负实数,那么这个单词就表示一个 价格 。
- 例如
"$100"
、"$23"
和"$6"
表示价格,而"100"
、"$"
和"$1e5
不是。
给你一个字符串 sentence
表示一个句子和一个整数 discount
。对于每个表示价格的单词,都在价格的基础上减免 discount%
,并 更新 该单词到句子中。所有更新后的价格应该表示为一个 恰好保留小数点后两位 的数字。
返回表示修改后句子的字符串。
注意:所有价格 最多 为 10
位数字。
示例 1:
输入:sentence = "there are $1 $2 and 5$ candies in the shop", discount = 50 输出:"there are $0.50 $1.00 and 5$ candies in the shop" 解释: 表示价格的单词是 "$1" 和 "$2" 。 - "$1" 减免 50% 为 "$0.50" ,所以 "$1" 替换为 "$0.50" 。 - "$2" 减免 50% 为 "$1" ,所以 "$1" 替换为 "$1.00" 。
示例 2:
输入:sentence = "1 2 $3 4 $5 $6 7 8$ $9 $10$", discount = 100 输出:"1 2 $0.00 4 $0.00 $0.00 7 8$ $0.00 $10$" 解释: 任何价格减免 100% 都会得到 0 。 表示价格的单词分别是 "$3"、"$5"、"$6" 和 "$9"。 每个单词都替换为 "$0.00"。
提示:
1 <= sentence.length <= 105
sentence
由小写英文字母、数字、' '
和'$'
组成sentence
不含前导和尾随空格sentence
的所有单词都用单个空格分隔- 所有价格都是 正 整数且不含前导零
- 所有价格 最多 为
10
位数字 0 <= discount <= 100
思路题解:
我们按照题目中的要求进行模拟即可。
首先我们将给定的字符串 sentence\textit{sentence}sentence 根据空格进行分割,得到其中的每一个单词。随后我们遍历每个单词,如果该单词:
以 $ 开头;
后续至少有一个字符,且均在 [0,9][0, 9][0,9] 中;
那么该单词就表示一个价格。我们提取后续的字符,转换成整数,计算折扣(即乘上 1−discount1001 - \dfrac{\textit{discount}}{100}1−100discount),保留两位小数,再转换回字符串,并添加开头的 $ 即可。
当所有单词遍历完成之后,我们就可以再加上空格,得到最终的字符串。
链接:https://leetcode.cn/problems/apply-discount-to-prices/solutions/2809596/jie-ge-jian-mian-by-leetcode-solution-m8tx/
来源:力扣(LeetCode)
作者:力扣官方题解
力扣官方题解
bool isNumeric(const char *s) {
int len = strlen(s);
if (len == 0) {
return false;
}
for (int i = 0; i < len; i++) {
if (!isdigit(s[i])) {
return false;
}
}
return true;
}char* discountPrices(char* sentence, int discount) {
char *ans = (char *)calloc(strlen(sentence) * 4, sizeof(char));
int pos = 0;
int len = strlen(sentence);
char *token = strtok(sentence, " ");
while (token != NULL) {
if (token[0] == '$' && isNumeric(token + 1)) {
double price = atol(token + 1) * (1 - discount / 100.0);
pos += sprintf(ans + pos, "$%0.2f ", price);
} else {
pos += sprintf(ans + pos, "%s ", token);
}
token = strtok(NULL, " ");
}
ans[pos - 1] = '\0';
return ans;
}
相关函数解析
calloc
语法:
#include <stdlib.h> void *calloc( size_t num, size_t size ); |
功能: 函数返回一个指向num 数组空间,每一数组元素的大小为size。如果错误发生返回NULL。
strtok
语法:
#include <string.h> char *strtok( char *str1, const char *str2 ); |
功能:函数返回字符串str1中紧接“标记”的部分的指针, 字符串str2是作为标记的分隔符。如果分隔标记没有找到,函数返回NULL。为了将字符串转换成标记,第一次调用str1 指向作为标记的分隔符。之后所以的调用str1 都应为NULL。
例如:
char str[] = "now # is the time for all # good men to come to the # aid of their country";
char delims[] = "#";
char *result = NULL;
result = strtok( str, delims );
while( result != NULL ) {
printf( "result is \"%s\"\n", result );
result = strtok( NULL, delims );
}
以上代码的运行结果是:
result is "now "
result is " is the time for all "
result is " good men to come to the "
result is " aid of their country"
atol
语法:
#include <stdlib.h> long atol( const char *str ); |
功能:将字符串转换成长整型数并返回结果。函数会扫描参数str字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,而再遇到非数字或字符串结束时才结束转换,并将结果返回。例如,
x = atol( "1024.0001" );
x的值为1024L.
IsNumeric
IsNumeric 函数返回 Boolean 值,表示表达式能否当作一个数,并且使用该数进行计算。
umeric 函数返回一个布尔值,指示指定的表达式是否可作为数字来计算。如果表达式作为数字来计算则返回 True ,否则返回 False。
注意:如果表达式是一个日期,IsNumeric 函数将返回 False。