题意:给出一个字符串,其是由0-9数字的英文打散后的,求其原始的数字

思路:刚开始用map来做,依次遍历zero,one,这样会有问题,可能有剩余的字符串不能构成数字的英文表示。

依赖唯一的字符数字表示有:zero(z), two(w), four(u), six(x), eight(g)

依赖一个的字符表示 有three(eight), seven(six), five(four)

依赖三个的字符有one(zero, two,four),nine(eight, six, five)

具体代码如下:

public class Solution
{public String originalDigits(String s){int[] num = new int[10];for (int i = 0; i < s.length(); i++){if (s.charAt(i) == 'z') num[0]++;else if (s.charAt(i) == 'w') num[2]++;else if (s.charAt(i) == 'u')  num[4]++;else if (s.charAt(i) == 'x') num[6]++;else if (s.charAt(i) == 'g') num[8]++;else if (s.charAt(i) == 'h') num[3]++;else if (s.charAt(i) == 's') num[7]++;else if (s.charAt(i) == 'f') num[5]++;else if (s.charAt(i) == 'o') num[1]++;else if (s.charAt(i) == 'i') num[9]++;}num[3] -= num[8];num[7] -= num[6];num[5] -= num[4];num[1] -= num[0] + num[2] + num[4];num[9] -= num[8] + num[6] + num[5];StringBuilder sb = new StringBuilder();for (int i = 0; i < 10; i++){for (int j = 0; j < num[i]; j++){sb.append(i);}}return sb.toString();}
}

LeetCode Reconstruct Original Digits from English相关推荐

  1. 【leetcode】423. Reconstruct Original Digits from English

    题目如下: Given a non-empty string containing an out-of-order English representation of digits 0-9, outp ...

  2. leetcode 423. Reconstruct Original Digits from English | 423. 从英文中重建数字(Java)

    题目 https://leetcode.com/problems/reconstruct-original-digits-from-english/ 题解 class Solution {public ...

  3. Reconstruct Original Digits from English 从英文中重建数字

    给定一个非空字符串,其中包含字母顺序打乱的英文单词表示的数字0-9.按升序输出原始的数字. 注意: 输入只包含小写英文字母. 输入保证合法并可以转换为原始的数字,这意味着像 "abc&quo ...

  4. LeetCode Reconstruct Itinerary(欧拉回路)

    问题:给出一些行程,要求输出从JFK出发的所有行程,如果有多个,要求行程是字典序最小的. 思路:欧拉回路问题. 具体代码参考: https://github.com/wuli2496/OJ/tree/ ...

  5. LeetCode Remove K Digits

    原题链接在这里:https://leetcode.com/problems/remove-k-digits/description/ 题目: Given a non-negative integer  ...

  6. 【LeetCode】273. Integer to English Words 解题报告

    转载请注明出处:http://blog.csdn.net/crazy1235/article/details/52756494 Subject 出处:https://leetcode.com/prob ...

  7. [leetcode][math] Add Digits

    题目: Given a non-negative integer num, repeatedly add all its digits until the result has only one di ...

  8. LeetCode之Add Digits

    1.题目 Given a non-negative integer num, repeatedly add all its digits until the result has only one d ...

  9. LeetCode 258 Add Digits

    leetcode 上做了一题比较有意思,记录一下,传送门:https://leetcode.com/problems/add-digits/description/ 题目意思是:给一个正整数a,让其个 ...

最新文章

  1. micro hdmi引脚定义义_臻实力芯定义:京东AMD笔记本电脑双11开门红-AMD笔记本 ——快科技(驱动之家旗下媒体)-...
  2. 关于整型数据符号位扩展的问题
  3. 全球及中国通信中的量子计算行业十四五规划方向与运营前景研究报告2022版
  4. 2019年Java架构师必读书籍
  5. 【搜索引擎基础知识2】网络爬虫的介绍
  6. (转)CentOS分区操作详解
  7. 考研计算机专业课复习,考研中计算机专业课的复习考试.pdf
  8. python数据库连接池使用
  9. [转载].gdb调试器快速入门
  10. Marco:Filecash全网算力增加的趋势,将形成FIC价格上升的良性循环
  11. 如何在Android中获取当前时间和日期
  12. GLCM opencv
  13. 母子关系:DAE是COLLADA的模型文件(转载)
  14. kernal tch 下载 天正_【T20天正插件下载】T20天正插件最新版 v5.0 官方版-开心电玩...
  15. labview入门系列2
  16. iframe背景透明的设置方法
  17. python 视频教程推荐_求各位大佬推荐Python学习视频教程?
  18. office 2010 Word,Excel的功能区突然消失了
  19. 【最新】半小时教你制作出属于自己的QQ机器人【保姆级】
  20. MySQL字符串数据类型

热门文章

  1. Failed to install*.apk on device '': timeout
  2. 高性能udp服务器架构,优秀的国产高性能TCP/UDP/HTTP开源网络通信框架——HP
  3. matlab编译错误怎么查找,matlab - Matlab mex文件编译错误-“未启用SSE2指令集” - 堆栈内存溢出...
  4. python快速编程入门第13章-Python快速编程入门,打牢基础必须知道的11个知识点...
  5. python没有英文基础能学吗-初中毕业没有英语基础能学编程吗?该学C或者Python?...
  6. python代码示例500行源代码-500行Python代码打造刷脸考勤系统,其实也就那么简单...
  7. python基础语法第10关作业-Python基础语法习题一
  8. python学到什么程度可以做兼职-Python学到什么程度可以面试工作(解答一)
  9. python绘制动态模拟图-Python 模拟动态产生验证码图片
  10. python爬虫简单实例-Python 利用Python编写简单网络爬虫实例3