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 }
  • 相关阅读:
    地址栏传值 JS取值方法
    定位导航 制作
    验证码
    图片水印
    AJAX 三级联动
    javascript 和Jquery 互转
    Jquery 事件 DOM操作
    Jquery 基础
    软件工程中的形式化方法读后感
    软件工程理论、方法与实践 需求工程读后感
  • 原文地址:https://www.cnblogs.com/fcyworld/p/6152932.html
Copyright © 2011-2022 走看看