当前位置:网站首页>2022.05.29(LC_6079_价格减免)
2022.05.29(LC_6079_价格减免)
2022-06-10 18:16:00 【Leeli9316】

方法:模拟
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);
}
//判断单词是否为价格(单词满足的条件:第一位是$,其他位是数字)
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;
}
}使用库函数:String.format(),String.join()。
边栏推荐
- 跨域报错:When allowCredentials is true, allowedOrigins cannot contain the special value “*“
- Chapter II data type (I)
- VIM common shortcut keys
- MySQL index invalidation scenario
- 单纯形法代码求解(含超详细代码注释和整个流程图)
- OPENCV 检测人脸 不依赖于任何第三方库
- [kuangbin] topic 22 interval DP
- Libcurl 7.61.0 vs2013 compilation tutorial
- Seata安装Window环境
- MySQL索引失效场景
猜你喜欢

Leecode27977 double finger needling

HelloWorld example of TestNG and how to run it from the command line

Adobe Premiere Basics - introduction, configuration, shortcut keys, creating projects, creating sequences (I)

TestNG的HelloWorld例子以及如何在命令行下运行

Introduction to ad18 device library import

Adobe Premiere基础-介绍,配置,快捷键,创建项目,创建序列(一)

How to set up salesmartly for Google Analytics tracking

数字化转型怎样转?朝哪转?

Adobe Premiere基础特效(卡点和转场)(四)

MySQL高级篇第一章(linux下安装MySQL)【上】
随机推荐
WordPress 6.0 "Arturo Arturo" release
Leecode27977 double finger needling
AEC:回声产生原因及回声消除原理解析
Adobe Premiere基础-工具使用(选择工具,剃刀工具,等常用工具)(三)
[kuangbin] topic 22 interval DP
Adobe Premiere基础-不透明度(混合模式)(十二)
Rewrite clear Bayesian formula with base ratio
MySQL索引失效场景
Ibox system development core functions and some core source codes
Db2 SQL PL的锚点类型和行数据类型
腾讯云数据库TDSQL-大咖论道 | 基础软件的过去、现在、未来
Cross domain error: when allowcredentials is true, allowedorigins cannot contain the special value "*“
openSSL1.1.1编译错误 Can‘t locate Win32/Console.pm in @INC
Array signal processing simulation part IV -- Z-transform analysis array polynomial
Win32 child window parent window window owner
How to play the Dragon Boat Festival "immersive cloud Tour"? That is to say, it helps "live broadcast +" new scene landing
Adobe Premiere foundation - tool use (selection tool, razor tool, and other common tools) (III)
Nodejs judge system type get host name execute console command Chinese garbled code
How to correctly understand the real-time nature of Bi?
跨域报错:When allowCredentials is true, allowedOrigins cannot contain the special value “*“