zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    初尝2D骨骼动画编辑工具SPINE,并into Unity3D
    Unity3D优化总结(一)
    ARPG游戏打击感相关的技术简单总结
    总结一下一般游戏中3D模型各种勾边方法遇到的工程性问题
    多次被坑了后,重温设计模式原则
    unity shader序列帧动画代码,顺便吐槽一下unity shader系统
    暗黑三泛光算法研究
    《全民无双》锁链刀光程序生成算法,即U3D——刀光轨迹插件的改进
    Low-level Thinking in High-level Shading Languages
    python实现智能语音天气预报
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/6132641.html
Copyright © 2011-2022 走看看