您的位置:首页 > 其它

423. Reconstruct Original Digits from English**

2017-01-19 11:21 465 查看
Given a non-empty string containing an out-of-order English representation of digits
0-9
, output the digits in ascending order.

Note:

Input contains only lowercase English letters.
Input is guaranteed to be valid and can be transformed to its original digits. That means invalid inputs such as "abc" or "zerone" are not permitted.
Input length is less than 50,000.
Example 1:

Input: "owoztneoer"

Output: "012"

Example 2:

Input: "fviefuro"

Output: "45"

public String originalDigits(String s) {
int[] result = new int[10];
for(int i=0;i<s.length();i++){
char c = s.charAt(i);
if(c=='z') result[0]++;
else if(c=='w') result[2]++;
else if(c=='x') result[6]++;
else if(c=='s') result[7]++;
else if(c=='g') result[8]++;
else if(c=='u') result[4]++;
else if(c=='f') result[5]++;
else if(c=='h') result[3]++;
else if(c=='i') result[9]++;
else if(c=='o') result[1]++;
}
result[7]-=result[6];
result[5]-=result[4];
result[3]-=result[8];
result[9]=result[9]-result[8]-result[5]-result[6];
result[1]=result[1]-result[0]-result[2]-result[4];
StringBuilder p = new StringBuilder();
for(int i=0;i<=9;i++){
for(int j=0;j<result[i];j++)
p.append(i);
}
return p.toString();
}
总结:纯找规律的一道题,耐心一点,不要什么都要想着捷径。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: