Leetcode: Reconstruct Original Digits from English

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 }
时间: 2024-10-09 18:04:51

Leetcode: Reconstruct Original Digits from English的相关文章

[LeetCode] Reconstruct Original Digits from English 从英文中重建数字

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 origina

LeetCode 423. Reconstruct Original Digits from English——学会观察,贪心思路

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 origina

423. Reconstruct Original Digits from English (leetcode)

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 origina

[LeetCode]423. Reconstruct Original Digits from English

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 origina

[Swift]LeetCode423. 从英文中重建数字 | Reconstruct Original Digits from English

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 origina

Leetcode-423 Reconstruct Original Digits from English(从英文中重建数字)

1 class Solution 2 { 3 public: 4 string originalDigits(string s) 5 { 6 vector<int> CharacterList(26,0); 7 for(auto c:s) 8 { 9 CharacterList[c-'a'] ++; 10 } 11 12 string result; 13 if(CharacterList['g'-'a']) 14 { 15 result.append(CharacterList['g'-'a

LeetCode:Add Digits - 非负整数各位相加

1.题目名称 Add Digits (非负整数各位相加) 2.题目地址 https://leetcode.com/problems/add-digits 3.题目内容 英文:Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. 中文:有一个非负整数num,重复这样的操作:对该数字的各位数字求和,对这个和的各位数字再求和--直到最后得到一个仅1位的数字

Leetcode: Reconstruct Itinerary

Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], reconstruct the itinerary in order. All of the tickets belong to a man who departs from JFK. Thus, the itinerary must begin with JFK. Note: If there ar

[LeetCode] Remove K Digits 去掉K位数字

Given a non-negative integer num represented as a string, remove k digits from the number so that the new number is the smallest possible. Note: The length of num is less than 10002 and will be ≥ k. The given num does not contain any leading zero. Ex