英文:
Replace one set of characters with another set of characters
问题
我正在编写代码,将罗马数字转换为十进制数字。
我想知道是否可以优化这一行?
String s = "MCMXCIV";
String code = s.replaceAll("I", "a")
.replaceAll("V", "b")
.replaceAll("X", "c")
.replaceAll("L", "d")
.replaceAll("C", "e")
.replaceAll("D", "f")
.replaceAll("M", "g");
预期的 code
值:gegceab
编辑 1:是否可以以更短的方式编写相同的代码?
编辑 2:
我希望满足两个条件:
- 避免昂贵的字符串连接
- 在
O(1)
时间内将每个字符替换为相应的代码
英文:
I am writing code to convert Roman Numbers to Decimal Numbers.
I was wondering if this line could be optimised?
String s = "MCMXCIV";
String code = s.replaceAll("I", "a")
.replaceAll("V", "b")
.replaceAll("X", "c")
.replaceAll("L", "d")
.replaceAll("C", "e")
.replaceAll("D", "f")
.replaceAll("M", "g");
Expected value of code
: gegceab
EDIT 1: Can the same code can be written in a shorter way?
EDIT 2:
Two conditions I want
- Avoid expensive string concatenation
- replace each character with corresponding code in
O(1)
答案1
得分: 1
我不知道这如何帮助你将罗马数字转换为十进制数字,但是,当然,这可以简化!
首先,.replace
在这方面做得更好(replaceAll
是一个愚蠢的名字;第一个参数是正则表达式。replace
,没有all
,也会替换所有内容,并且采用实际的原始字符串,这正是你想要的 – Java 不喜欢破坏向后兼容性;这个愚蠢的名字不太可能被更正)。
其次,“更短”在大多数情况下没有意义。编写代码时相关的目标是可读性和灵活性,很少关注效率(性能);更短本身不是一个相关的目标。
这里是一种替代方法;对于大字符串来说,效率更高。对于小字符串来说,现在已经是2020年,所需时间会接近于0。
private static final String LETTERS = "IVXLCDM";
char[] cs = s.toCharArray();
for (int i = 0; i < cs.length; i++) {
cs[i] = 'a' + LETTERS.indexOf(cs[i]);
}
String code = new String(cs);
英文:
I have no idea how this helps you translate roman numbers to decimal numbers, but, sure, this can be simplified!
For starters, .replace
does this job strictly better ('replaceAll
is a stupid name; the first argument is a regular expression. replace
, without the all, also replaces all, and takes an actual raw string, which is what you wanted – java doesn't like breaking backwards compatibility; the silly name is unlikely to be corrected).
Secondly, 'shorter' is mostly meaningless. The relevant goals when writing code are readability and flexibility, and rarely efficiency (performance); shorter is on its own not a relevant goal.
Here's an alternative take; it's significantly more efficient for large strings. For small strings, it's 2020, the time taken is going to round down to 0.
private static final String LETTERS = "IVXLCDM";
char[] cs = s.toCharArray();
for (int i = 0; i < cs.length; i++) {
cs[i] = 'a' + LETTERS.indexOf(cs[i]);
}
String code = new String(cs);
答案2
得分: 1
你可以使用两个数组并循环遍历它们,将from[i]
替换为to[i]
。
String s = "MCMXCIV";
char[] from = new char[]{'I', 'V', 'X', 'L', 'C', 'D', 'M'};
char[] to = new char[]{'a', 'b', 'c', 'd', 'e', 'f', 'g'};
for (int i = 0; i < from.length; i++){
s = s.replace(from[i], to[i]);
}
英文:
You can use two arrays and loop through them, replacing from[i]
with to[i]
.
String s = "MCMXCIV";
char[] from = new char[]{'I', 'V', 'X', 'L', 'C', 'D', 'M'};
char[] to = new char[]{'a', 'b', 'c', 'd', 'e', 'f', 'g'};
for (int i = 0; i < from.length; i++){
s = s.replace(from[i], to[i]);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论