首页 > 其他 > 详细

Leetcode: Reconstruct Original Digits from English

时间:2016-12-05 09:28:39      阅读:326      评论:0      收藏:0      [点我收藏+]
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"

# of ‘0‘: # of ‘z‘

# of ‘2‘: # of ‘w‘

4: u

6: x

8: g

3: h - 8

5: f - 4

7: s - 6

1: o - 0 - 2 - 4

9: i - 5 - 6 - 8

 1 public String originalDigits(String s) {
 2     int[] count = new int[10];
 3     for (int i = 0; i < s.length(); i++){
 4         char c = s.charAt(i);
 5         if (c == ‘z‘) count[0]++;
 6         if (c == ‘w‘) count[2]++;
 7         if (c == ‘x‘) count[6]++;
 8         if (c == ‘s‘) count[7]++; //7-6
 9         if (c == ‘g‘) count[8]++;
10         if (c == ‘u‘) count[4]++; 
11         if (c == ‘f‘) count[5]++; //5-4
12         if (c == ‘h‘) count[3]++; //3-8
13         if (c == ‘i‘) count[9]++; //9-8-5-6
14         if (c == ‘o‘) count[1]++; //1-0-2-4
15     }
16     count[7] -= count[6];
17     count[5] -= count[4];
18     count[3] -= count[8];
19     count[9] = count[9] - count[8] - count[5] - count[6];
20     count[1] = count[1] - count[0] - count[2] - count[4];
21     StringBuilder sb = new StringBuilder();
22     for (int i = 0; i <= 9; i++){
23         for (int j = 0; j < count[i]; j++){
24             sb.append(i);
25         }
26     }
27     return sb.toString();
28 }

 

我的code用了一个数组来存char count

 1 public class Solution {
 2     public String originalDigits(String s) {
 3         StringBuilder res = new StringBuilder();
 4         if (s==null || s.length()==0) return "";
 5         int[] chars = new int[26];
 6         int[] digits = new int[10];
 7         for (int i=0; i<s.length(); i++) {
 8             chars[s.charAt(i)-‘a‘]++;
 9         }
10         count(chars, digits);
11         for (int i=0; i<digits.length; i++) {
12             for (int j=0; j<digits[i]; j++) {
13                 res.append(i);
14             }
15         }
16         return res.toString();
17     } 
18     
19     public void count(int[] chars, int[] digits) {
20         //‘0‘
21         digits[0] = chars[‘z‘-‘a‘];
22         //‘2‘
23         digits[2] = chars[‘w‘-‘a‘];
24         //‘4‘
25         digits[4] = chars[‘u‘-‘a‘];
26         //‘6‘
27         digits[6] = chars[‘x‘-‘a‘];
28         //‘8‘
29         digits[8] = chars[‘g‘-‘a‘];
30         //‘1‘ and ‘2‘ and ‘0‘ and ‘4‘ share ‘o‘
31         digits[1] = chars[‘o‘-‘a‘] - digits[2] - digits[0] - digits[4];
32         //‘3‘ and ‘8‘ share ‘h‘
33         digits[3] = chars[‘h‘-‘a‘] - digits[8];
34         //‘5‘ and ‘4‘ share ‘f‘
35         digits[5] = chars[‘f‘-‘a‘] - digits[4];
36         //‘7‘ and ‘6‘ share ‘s‘
37         digits[7] = chars[‘s‘-‘a‘] - digits[6];
38         //‘9‘ and ‘5‘ and ‘6‘ and ‘8‘ share ‘i‘
39         digits[9] = chars[‘i‘-‘a‘] - digits[5] - digits[6] - digits[8];
40     }
41 }

 

Leetcode: Reconstruct Original Digits from English

原文:http://www.cnblogs.com/EdwardLiu/p/6132641.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!