LeetCode Reconstruct Original Digits from English
2016-10-17 18:56
537 查看
题意:给出一个字符串,其是由0-9数字的英文打散后的,求其原始的数字
思路:刚开始用map来做,依次遍历zero,one,这样会有问题,可能有剩余的字符串不能构成数字的英文表示。
依赖唯一的字符数字表示有:zero(z), two(w), four(u), six(x), eight(g)
依赖一个的字符表示 有three(eight), seven(six), five(four)
依赖三个的字符有one(zero, two,four),nine(eight, six, five)
具体代码如下:
public class Solution
{
public String originalDigits(String s)
{
int[] num = new int[10];
for (int i = 0; i < s.length(); i++)
{
if (s.charAt(i) == 'z') num[0]++;
else if (s.charAt(i) == 'w') num[2]++;
else if (s.charAt(i) == 'u') num[4]++;
else if (s.charAt(i) == 'x') num[6]++;
else if (s.charAt(i) == 'g') num[8]++;
else if (s.charAt(i) == 'h') num[3]++;
else if (s.charAt(i) == 's') num[7]++;
else if (s.charAt(i) == 'f') num[5]++;
else if (s.charAt(i) == 'o') num[1]++;
else if (s.charAt(i) == 'i') num[9]++;
}
num[3] -= num[8];
num[7] -= num[6];
num[5] -= num[4];
num[1] -= num[0] + num[2] + num[4];
num[9] -= num[8] + num[6] + num[5];
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < num[i]; j++)
{
sb.append(i);
}
}
return sb.toString();
}
}
思路:刚开始用map来做,依次遍历zero,one,这样会有问题,可能有剩余的字符串不能构成数字的英文表示。
依赖唯一的字符数字表示有:zero(z), two(w), four(u), six(x), eight(g)
依赖一个的字符表示 有three(eight), seven(six), five(four)
依赖三个的字符有one(zero, two,four),nine(eight, six, five)
具体代码如下:
public class Solution
{
public String originalDigits(String s)
{
int[] num = new int[10];
for (int i = 0; i < s.length(); i++)
{
if (s.charAt(i) == 'z') num[0]++;
else if (s.charAt(i) == 'w') num[2]++;
else if (s.charAt(i) == 'u') num[4]++;
else if (s.charAt(i) == 'x') num[6]++;
else if (s.charAt(i) == 'g') num[8]++;
else if (s.charAt(i) == 'h') num[3]++;
else if (s.charAt(i) == 's') num[7]++;
else if (s.charAt(i) == 'f') num[5]++;
else if (s.charAt(i) == 'o') num[1]++;
else if (s.charAt(i) == 'i') num[9]++;
}
num[3] -= num[8];
num[7] -= num[6];
num[5] -= num[4];
num[1] -= num[0] + num[2] + num[4];
num[9] -= num[8] + num[6] + num[5];
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < num[i]; j++)
{
sb.append(i);
}
}
return sb.toString();
}
}
相关文章推荐
- Leetcode: Reconstruct Original Digits from English
- LeetCode No.423 Reconstruct Original Digits from English
- [LeetCode] Reconstruct Original Digits from English 从英文中重建数字
- [LeetCode] Reconstruct Original Digits from English
- LeetCode-Reconstruct Original Digits from English
- [LeetCode] 423 Reconstruct Original Digits from English
- leetcode 423 - Reconstruct Original Digits from English
- LeetCode: Reconstruct Original Digits from English
- 423. Reconstruct Original Digits from English
- LeetCode - 423. Reconstruct Original Digits from English
- LeetCode 423. Reconstruct Original Digits from English
- LeetCode 423. Reconstruct Original Digits from English 解题报告
- Leetcode-423. Reconstruct Original Digits from English
- [LeetCode] 423. Reconstruct Original Digits from English 解题报告
- LeetCode 423. Reconstruct Original Digits from English解题笔记
- Reconstruct Original Digits from English
- LeetCode 423. Reconstruct Original Digits from English
- Math-423-Reconstruct Original Digits from English
- leetcode 423. Reconstruct Original Digits from English
- LeetCode 423. Reconstruct Original Digits from English