2288、价格减免

  1. 题目
    1. 示例 1:
    2. 示例 2:
  2. 分析
    1. 枚举匹配 处理
    2. 原地修改中字符串
    3. 手动转换数字,执行折算

题目

句子 是由若干个单词组成的字符串,单词之间用单个空格分隔,其中每个单词可以包含数字、小写字母、和美元符号 ‘$’ 。如果单词的形式为美元符号后跟着一个非负实数,那么这个单词就表示一个 价格 。

例如 “$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 <= 10^5
  • sentence 由小写英文字母、数字、’ ‘ 和 ‘$’ 组成
  • sentence 不含前导和尾随空格
  • sentence 的所有单词都用单个空格分隔
  • 所有价格都是 正 整数且不含前导零
  • 所有价格 最多 为 10 位数字
  • 0 <= discount <= 100

分析

  • 字符串匹配
  • 数字识别

枚举匹配 处理

使用stringBuilder

class Solution {
    public String discountPrices(String sentence, int discount) {
        // str to double
        String[] sen = sentence.split(" ");
        StringBuilder newSen = new StringBuilder();
        for (String s: sen) {
            if (s.startsWith("$")) {
                String num = s.substring(1,s.length());
                if (!isNumber(num) || num.isEmpty()) {
                    newSen.append(s+ " ");
                    continue;
                }
                double num1 = Double.valueOf(num) * (1.00 -(double)discount/100.00);
                newSen.append(String.format("$%.2f",num1) + " ");
            } else {
                newSen.append(s+ " ");
            }
        }
        return newSen.toString().strip();
    }

    private boolean isNumber(String num) {
        for (char ch:num.toCharArray()) {
            if (ch < '0' || ch > '9') {
                return false;
            }
        }

        return true;
    }
}

原地修改中字符串

class Solution {
    public String discountPrices(String sentence, int discount) {
        // str to double
        String[] sen = sentence.split(" ");
        for (int i = 0;i < sen.length; i++) {
            if (sen[i].startsWith("$")) {
                String num = sen[i].substring(1,sen[i].length());
                if (!isNumber(num) || num.isEmpty()) {
                    continue;
                }
                double num1 = Double.valueOf(num) * (1.00 -(double)discount/100.00);
                sen[i] = String.format("$%.2f",num1);
            }
        }
        return String.join(" ",sen);
    }

    private boolean isNumber(String num) {
        for (char ch:num.toCharArray()) {
            if (ch < '0' || ch > '9') {
                return false;
            }
        }

        return true;
    }
}

手动转换数字,执行折算

  • 使用StringBuilders
  • 对于每个字符串数字
    • 字符串整数
    • 执行折扣运算
      • 取商-拼接
      • 去余-拼接
class Solution {
    public String discountPrices(String sentence, int discount) {
        
        String[] words = sentence.split(" ");
        int n = words.length;
        int  d = 100 - discount;
         
       StringBuilder result = new StringBuilder();
         
        for(int i = 0; i < n; ++i){
            
            String word = words[i];
            if(word.length() < 2 || word.charAt(0) != '$'){
                result.append(' ').append(words[i]);
                continue;
            }
            
            String num = word.substring(1);
            long amount = getNum(num);
            if(amount != -1){
                amount *= d;
                result.append(' ').append('$').append(amount/100).append('.');
                amount %= 100;
                if(amount < 10)
                   result.append('0');
                result.append(amount);
            }else{
                result.append(' ').append(words[i]);
            }
        }
       
        return result.toString().substring(1);
    }
    
    private long getNum(String num){
        
        long amount = 0;
        for(char c:num.toCharArray()){
           if(c < '0' || c > '9')
              return -1;
            amount = amount * 10 + (c - '0');
        }
        return amount;
    }
}

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 1056615746@qq.com

💰

Title:2288、价格减免

Count:935

Author:攀登

Created At:2024-06-18, 08:42:51

Updated At:2024-06-18, 08:54:11

Url:http://jiafeimao-gjf.github.io/2024/06/18/2288%E3%80%81%E4%BB%B7%E6%A0%BC%E5%87%8F%E5%85%8D/

Copyright: 'Attribution-non-commercial-shared in the same way 4.0' Reprint please keep the original link and author.

×

Help us with donation