[LeetCode] 2288. Apply Discount to Prices
作者:互联网
A sentence is a string of single-space separated words where each word can contain digits, lowercase letters, and the dollar sign '$'
. A word represents a price if it is a non-negative real number preceded by a dollar sign.
- For example,
"$100"
,"$23"
, and"$6.75"
represent prices while"100"
,"$"
, and"2$3"
do not.
You are given a string sentence
representing a sentence and an integer discount
. For each word representing a price, apply a discount of discount%
on the price and update the word in the sentence. All updated prices should be represented with exactly two decimal places.
Return a string representing the modified sentence.
Example 1:
Input: sentence = "there are $1 $2 and 5$ candies in the shop", discount = 50 Output: "there are $0.50 $1.00 and 5$ candies in the shop" Explanation: The words which represent prices are "$1" and "$2". - A 50% discount on "$1" yields "$0.50", so "$1" is replaced by "$0.50". - A 50% discount on "$2" yields "$1". Since we need to have exactly 2 decimal places after a price, we replace "$2" with "$1.00".
Example 2:
Input: sentence = "1 2 $3 4 $5 $6 7 8$ $9 $10$", discount = 100 Output: "1 2 $0.00 4 $0.00 $0.00 7 8$ $0.00 $10$" Explanation: Applying a 100% discount on any price will result in 0. The words representing prices are "$3", "$5", "$6", and "$9". Each of them is replaced by "$0.00".
Constraints:
1 <= sentence.length <= 105
sentence
consists of lowercase English letters, digits,' '
, and'$'
.sentence
does not have leading or trailing spaces.- All words in
sentence
are separated by a single space. - All prices will be positive integers without leading zeros.
- All prices will have at most
10
digits. 0 <= discount <= 100
价格减免。
句子 是由若干个单词组成的字符串,单词之间用单个空格分隔,其中每个单词可以包含数字、小写字母、和美元符号 '$' 。如果单词的形式为美元符号后跟着一个非负实数,那么这个单词就表示一个价格。
例如 "$100"、"$23" 和 "$6.75" 表示价格,而 "100"、"$" 和 "2$3" 不是。
注意:本题输入中的价格均为整数。给你一个字符串 sentence 和一个整数 discount 。对于每个表示价格的单词,都在价格的基础上减免 discount% ,并 更新 该单词到句子中。所有更新后的价格应该表示为一个 恰好保留小数点后两位 的数字。
返回表示修改后句子的字符串。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/apply-discount-to-prices
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
这是一道字符串处理的题目,思路不难但是需要细心。
题目说了句子是由一系列单词组成的,单词与单词之间是由空格分隔开的。单词的定义以及价格的定义参见题目描述。
思路是既然句子是由单词组成的,而单词与单词之间一定是由空格分开的,那么我们可以先按照空格将句子拆解成一个 String[] ,再遍历这个 string 数组去处理每一个单词。对于每一个单词,如果他是一个合法的表示价格的单词,那么他的结构一定是"$" + 纯数字。如果不满足这个格式,则只能将其当做一般的单词直接加入结果集。所以这里我们写一个 isValid() 函数去判断每个单词是否是一个合法的表示价格的单词。之后我们再进行 discount 的处理和最后字符串的拼接。注意处理 discount 的时候需要用到 double 同时注意精度问题。
时间O(n) - 字符串的总长度
空间O(n) - string array
Java实现
1 class Solution { 2 public String discountPrices(String sentence, int discount) { 3 String[] words = sentence.split(" "); 4 for (int i = 0; i < words.length; i++) { 5 String word = words[i]; 6 // corner case 7 if (word.length() < 2) { 8 continue; 9 } 10 11 // 单词与单词之间被空格分开 12 // 每个合法的钱的数目一定是$ + word的结构 13 char sign = word.charAt(0); 14 String num = word.substring(1); 15 if (isValid(sign, num)) { 16 words[i] = format(num, discount); 17 } 18 } 19 return String.join(" ", words); 20 } 21 22 // 没有$的符号开头就是错的 23 // 数字的部分只有整数 24 private boolean isValid(char sign, String num) { 25 if (sign != '$') { 26 return false; 27 } 28 for (char c : num.toCharArray()) { 29 if (c < '0' || c > '9') { 30 return false; 31 } 32 } 33 return true; 34 } 35 36 // 计算discount并格式化 37 private String format(String num, int discount) { 38 double d = Double.parseDouble(num) * (1 - discount / 100d); 39 return '$' + String.format("%.2f", d); 40 } 41 }
标签:2288,word,String,sentence,单词,discount,words,Prices,Apply 来源: https://www.cnblogs.com/cnoodle/p/16325549.html