当前位置:网站首页>2022.05.29 (lc_6079_price reduction)
2022.05.29 (lc_6079_price reduction)
2022-06-10 19:43:00 【Leeli9316】

Method : simulation
import java.text.DecimalFormat;
class Solution {
public String discountPrices(String sentence, int discount) {
String[] str = sentence.split(" ");
for (int i = 0; i < str.length; i++) {
if (satisfy(str[i])) {
java.text.DecimalFormat df = new java.text.DecimalFormat("#0.00");
double num = Double.valueOf(str[i].substring(1, str[i].length()));
num = num - (num * ((double)discount / 100));
str[i] = '$' + df.format(num);
}
}
String ans = "";
for (int i = 0; i < str.length; i++) {
if (i != str.length - 1) {
ans += str[i] + " ";
} else {
ans += str[i];
}
}
return ans;
}
public boolean satisfy(String s) {
if (s.charAt(0) != '$') return false;
if (s.charAt(s.length() - 1) == '$') return false;
int count$ = 0, countD = 0, countC = 0;
for (char ch : s.toCharArray()) {
if (ch == '$') {
count$++;
} else if (Character.isDigit(ch)) {
countD++;
} else {
countC++;
}
}
if (count$ != 1 || countC > 0) return false;
return true;
}
}class Solution {
public String discountPrices(String sentence, int discount) {
String[] s = sentence.split(" ");
for (int i = 0; i < s.length; i++) {
if (satisfy(s[i])) {
double num = Double.valueOf(s[i].substring(1));
num = num * (1 - (double)discount / 100);
s[i] = "$" + String.format("%.2f", num);
}
}
return String.join(" ", s);
}
// Judge whether the word is price ( Words meet the conditions : The first is $, Other bits are numbers )
public boolean satisfy(String s) {
if (s.charAt(0) != '$' || s.length() == 1) return false;
for (int i = 1; i < s.length(); i++) {
if (s.charAt(i) < '0' || s.charAt(i) > '9') {
return false;
}
}
return true;
}
}Use library functions :String.format(),String.join().
边栏推荐
- [6.4-6.10] wonderful review of Blog
- 【C语言】一不小心写出bug?凡人教你如何写出好代码【详解vs中调试技巧】
- Ranked first in China's SDN (software) market share for six consecutive years
- c(指针-02)
- SAR回波信号基本模型与性质
- Apicloud visual development - one click generation of professional source code
- 【C语言】还搞不明白结构体吗?不妨来看看这篇文章,带你初步了解结构体
- HW蓝队中级面试复盘
- 【web】個人主頁web大作業「課錶」「相册」「留言板」
- Ding Dong grabs vegetables - monitoring and pushing tools for delivery period
猜你喜欢

SAR image focusing quality evaluation plug-in

【web】个人主页web大作业「课表」「相册」「留言板」

SAR图像聚焦质量评价插件

Low carbon data center construction ideas and future trends

Basic improvement - tree DP supplement

APICloud可视化开发丨一键生成专业级源码

Apicloud visual development novice graphic tutorial

Yuntu says that every successful business system cannot be separated from apig
![[C language] still don't understand the structure? Take a look at this article to give you a preliminary understanding of structure](/img/94/c9c7935aa0c98eb39a34377ad02b10.png)
[C language] still don't understand the structure? Take a look at this article to give you a preliminary understanding of structure

基于改进SEIR模型分析上海疫情
随机推荐
面试中经常问到的几个问题,快来看看能答对几道吧
【C语言进阶】指针的进阶【上篇】
腾讯Libco协程开源库 源码分析(二)---- 柿子先从软的捏 入手示例代码 正式开始探究源码
c(指针-02)
Computer: successfully teach you how to use one trick to retrieve the previous password (the password once saved but currently displayed as ******)
Cet article vous donne un aperçu de la tâche future de j.u.c, du cadre Fork / join et de la file d'attente de blocage
Ranked first in China's SDN (software) market share for six consecutive years
DataScience&ML:金融科技领域之风控之风控指标/字段相关概念、口径逻辑之详细攻略
c(指针02)
LeetCode_ Concurrent search set_ Medium_ 399. division evaluation
腾讯Libco协程开源库 源码分析(三)---- 探索协程切换流程 汇编寄存器保存 高效保存协程环境
Monotonic stack structure
Source code analysis and practical testing openfeign load balancing
云图说|每个成功的业务系统都离不开APIG的保驾护航
Docker/Rancher2部署redis:5.0.9
mixin-- 混入
【C语言】这些经典题型大家都掌握了吗?一文学会这些题
一文带你了解J.U.C的FutureTask、Fork/Join框架和BlockingQueue
腾讯Libco协程开源库 源码分析 全系列总结博客
VS从txt文件读取中文汉字产生乱码的解决办法(超简单)