zoukankan      html  css  js  c++  java
  • 【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]='';
    34     return str;
    35 }
  • 相关阅读:
    修改profile出错后的补救
    ubuntu安装jdk
    创业的36条军规
    Oracle中慎用Like等通配符
    根据配置文件名读取配置文件的工具类方法
    Spring ThreadPoolTaskExecutor队列满的异常处理
    redis使用方法
    在windows 2008 R2中SQl Server 2008中代理启动失败的一个原因总结
    select 中添加option的注意
    javascript 函数对象
  • 原文地址:https://www.cnblogs.com/fcyworld/p/6152932.html
Copyright © 2011-2022 走看看