[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:

  1. Input contains only lowercase English letters.
  2. 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.
  3. Input length is less than 50,000.

Example 1:

Input: "owoztneoer"

Output: "012"

Example 2:

Input: "fviefuro"

Output: "45"

题意:给出一串乱序的数字单词,输出从小到大排序的对应数字

思路:

  利用暗藏的信息,找出每个数字的英文单词对应的特别的字母,比如two中的w,four中的u,eight中的g,six的x,zero的z,都是唯一的

  three的h,five的f,seven的s出现了两次

  one中的o出现了四次

  nine中的i出现了三次

利用这些特点,把这些字母作为所对应数字的唯一标示进行统计,之后进行数学处理,比如three中的h同时再eight中出现了,那么three的数量减去

eight的数量,就是three的真实数量

C代码如下:

 1 char* originalDigits(char* s) {
 2     int count[10]={0};
 3     int i=0,j=0;
 4     char *str=malloc(sizeof(char)*16000); //注意只能返回栈空间的变量
 5     while(s[i])
 6     {
 7         if(‘z‘==s[i]) count[0]++;
 8         else if (‘w‘==s[i]) count[2]++;
 9         else if (‘u‘==s[i]) count[4]++;
10         else if (‘x‘==s[i]) count[6]++;
11         else if (‘g‘==s[i]) count[8]++;
12         else if (‘f‘==s[i]) count[5]++;
13         else if (‘h‘==s[i]) count[3]++;
14         else if (‘s‘==s[i]) count[7]++;
15         else if (‘i‘==s[i]) count[9]++;
16         else if (‘o‘==s[i]) count[1]++;
17         i++;
18     }
19     count[5]-=count[4];
20     count[3]-=count[8];
21     count[7]-=count[6];
22     count[1]-=(count[2]+count[4]+count[0]);
23     count[9]-=(count[5]+count[6]+count[8]);
24     for(i=0;i<10;i++)
25         {
26             if(count[i])
27                 while(count[i]--)
28                 {
29                     str[j]=‘0‘+i;
30                     j++;
31                 }
32         }
33     str[j]=‘\0‘;
34     return str;
35 }
时间: 2024-12-15 06:55:20

[LeetCode]423. Reconstruct Original Digits from English的相关文章

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] 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: 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 332 Reconstruct Itinerary

题目连接 https://leetcode.com/problems/reconstruct-itinerary/ Reconstruct Itinerary Description 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 belo

LeetCode No.258 Add Digits

LeetCode  Algorithum # title solution difficulty 258  Add Digits  java  easy         NO.258 (2015.10.26 16:09:00) Given a non-negative integer num,repeatedly add all its digits until the result has only one digit. For example: Given num = 38, the pro

我也来刷LeetCode——3、Add Digits

唉,做完这道题目后,有种羞愧无比的感觉,果然还是读书太少... 什么意思呢?就像这道题目一样,要求你编程计算出从1加到100的结果.于是你用循环从1累加到100,结果为5050.而大家都知道,从数学上的角度来解答,结果就是(a[1]+a[n])* n / 2. LeetCode上面的这道[Add Digits]题,其实是道数学题.它这个求解的结果,在数学上称为数根. 原题大概意思是这样,给定一个正整数,循环累加它的各位,直到结果为一位数.如 num 是38,计算的过程是 3+8=11,1+1=2