用网站模板做新网站,西宁网站设计公司价格,oss可以做视频网站吗,去除wordpress主题版权句子 是由若干个单词组成的字符串#xff0c;单词之间用单个空格分隔#xff0c;其中每个单词可以包含数字、小写字母、和美元符号 $ 。如果单词的形式为美元符号后跟着一个非负实数#xff0c;那么这个单词就表示一个 价格 。
例如 $100、$23 和 …句子 是由若干个单词组成的字符串单词之间用单个空格分隔其中每个单词可以包含数字、小写字母、和美元符号 $ 。如果单词的形式为美元符号后跟着一个非负实数那么这个单词就表示一个 价格 。
例如 $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 所以 $2 替换为 $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 105sentence 由小写英文字母、数字、 和 $ 组成sentence 不含前导和尾随空格sentence 的所有单词都用单个空格分隔所有价格都是 正 整数且不含前导零所有价格 最多 为 10 位数字0 discount 100
问题简要描述返回修改后句子的字符串
Java
class Solution {public String discountPrices(String sentence, int discount) {String[] word sentence.split( );for (int i 0; i word.length; i) {if (check(word[i])) {double t Long.parseLong(word[i].substring(1)) * (1 - discount / 100.0);word[i] String.format($%.2f, t);}}return String.join( , word);}boolean check(String word) {if (word.charAt(0) ! $ || word.length() 1) {return false;}for (int i 1; i word.length(); i) {if (!Character.isDigit(word.charAt(i))) {return false;}}return true;}
} Python3
class Solution:def discountPrices(self, sentence: str, discount: int) - str:ans []for w in sentence.split( ):if w[0] $ and w[1:].isdigit():w f${int(w[1:]) * (1 - discount / 100):.2f}ans.append(w)return .join(ans)
TypeScript
function discountPrices(sentence: string, discount: number): string {let ans []const isDigit (word: string) {return /^\d$/.test(word);}for (let word of sentence.split( )) {if (word[0] $ isDigit(word.substring(1))) {let price parseInt(word.substring(1)) * (1 - discount / 100);word $${price.toFixed(2)};}ans.push(word);}return ans.join( );
};
Go
func discountPrices(sentence string, discount int) string {words : strings.Split(sentence, )for i, w : range words {if w[0] $ {if v, err : strconv.Atoi(w[1:]); err nil {words[i] fmt.Sprintf($%.2f, float64(v*(100-discount))/100)}}}return strings.Join(words, )
}