用一个字符集替换另一个字符集。

huangapple go评论119阅读模式
英文:

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

我希望满足两个条件:

  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

  1. Avoid expensive string concatenation
  2. 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 = &quot;IVXLCDM&quot;;

char[] cs = s.toCharArray();
for (int i = 0; i &lt; cs.length; i++) {
    cs[i] = &#39;a&#39; + 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 = &quot;MCMXCIV&quot;;
char[] from = new char[]{&#39;I&#39;, &#39;V&#39;, &#39;X&#39;, &#39;L&#39;, &#39;C&#39;, &#39;D&#39;, &#39;M&#39;};
char[] to = new char[]{&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;e&#39;, &#39;f&#39;, &#39;g&#39;};

for (int i = 0; i &lt; from.length; i++){
    s = s.replace(from[i], to[i]);
}

huangapple
  • 本文由 发表于 2020年8月3日 09:57:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63222793.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定