273. Integer to English Words

2023-10-07 20:48
文章标签 integer words 273 english

本文主要是介绍273. Integer to English Words,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

问题链接:https://leetcode.com/problems/integer-to-english-words/?tab=Description

Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1.

For example,

123 -> "One Hundred Twenty Three"
12345 -> "Twelve Thousand Three Hundred Forty Five"
1234567 -> "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"

Hint:

  1. Did you see a pattern in dividing the number into chunk of words? For example, 123 and 123000.
  2. Group the number by thousands (3 digits). You can write a helper function that takes a number less than 1000 and convert just that chunk to words.
  3. There are many edge cases. What are some good test cases? Does your code work with input such as 0? Or 1000010? (middle chunk is zero and should not be printed out)

想了半天还是没什么思路,参照答案默写如下:

public class Solution {private String TENS[] = {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};private String LESS_THAN_TWENTY[] = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};private String THOUSANDS[] = {"", "Thousand", "Million", "Billion"};public String numberToWords(int num) {if(num == 0) return "Zero";String result = "";int i = 0;while(num > 0) {if(num%1000 != 0)result = helper(num%1000) + THOUSANDS[i] + " " + result;num = num / 1000;i++;}return result.trim();}private String helper(int value) {if(value == 0)return "";else if(value < 20) {return LESS_THAN_TWENTY[value] + " ";} else if(value < 100) {return TENS[value/10] + " " + helper(value%10);} else {return LESS_THAN_TWENTY[value/100] + " Hundred " + helper(value%100);}}
}


这篇关于273. Integer to English Words的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/160334

相关文章

HDUPlay on Words

1.定理:无向图G有欧拉通路的充分必要条件是G为连通图,并且G仅有两个奇度结点或者无奇度结点。 (1)当G是仅有两个奇度结点的连通图时,G的欧拉通路必以此两个结点为端点。 (2)当G是无奇度结点的连通图时,G必有欧拉回路。 2.一个有向图D具有欧拉通路,当且仅当D是连通的,且除了两个顶点外,其余顶点的入度均等于出度,这两个特殊的顶点中,一个顶点的入度比出度大1,另一个顶点的入度比出度小1

如何简便的将List<Integer>转换成int[]?

使用Java 8的流(Streams)  ArrayList<Integer> list = new ArrayList<>();int[] intArray = list.stream().mapToInt(Integer::intValue).toArray();  若是maven项目可使用Apache Commons Lang库 <dependency> <groupId>

[LeetCode] 7. Reverse Integer

题:https://leetcode.com/problems/reverse-integer/description/ 题目 Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123Output: 321Example 2:Input: -123Output: -321Ex

[LeetCode] 692. Top K Frequent Words

题:https://leetcode.com/problems/top-k-frequent-words/ 题目大意 对于 string[] words,输出 出现频率前k高的 word,顺序 为 word 出现的频率 由高到低 ,频率相同的 word 按 字符排序。 思路 其实是对words中的所有word进行一个排序。 排序有两个规则: 1.word 在 words中出现的次数。 2.

[LeetCode] 820. Short Encoding of Words

题:https://leetcode.com/problems/short-encoding-of-words/ 题目大意 参考题目 思路 set 集合 将所有word 放入set中,然后遍历所有set中的word,将word的从头的子串都从set中删除,最后统计 set中所有(word 的长度 + 1)(’#’) class Solution {public int minimumL

LeetCode - 12. Integer to Roman

12. Integer to Roman  Problem's Link  ---------------------------------------------------------------------------- Mean:  将一个int型的整数转化为罗马数字. analyse: 没什么好说的,直接维基百科. Time complexity: O(

【HDU】4117 GRE Words AC自动机+线段树优化DP

传送门:【HDU】4117 GRE Words 题目分析:水不了啊狸的打字机就来水这题了= =。。。 首先建立ac自动机,然后用fail指针的反向关系建边,构造fail指针树。fail指针树中每个结点u表示的串都是其子节点v的后缀(同时该后缀是所有串中最长的)。对fail指针树dfs一次得到时间戳,当要求以串i结尾的最大价值,首先我们需要知道以串i的子串j结尾的最大价值val。因为在树中

带小数的String转整数Integer

其实String和Integer、Float、Double等相互转换这都很容易。可是带小数的String转Float、Double可能会出现“模糊数字”。 那么怎么避免呢?见下实例和结论。 System.out.println("**********2.4***********");String a = "2.4"; System.out.println(a); // 2.4System.o

LeetCode 30 Substring with Concatenation of All Words

题意: 给出字符串s和许多等长(len)单词w,找出所有s中的满足子串为w中所有单词的一种组合的位置。 思路: 因为w中的单词要满足的是组合而不是排列,因此用“区间[L,R]中包含单词的计数”来维护比较合适。 一是满足了组合对顺序的不要求,二是方便处理重复的单词。 首先可以统计一下,w中各种单词个数。如果s的长度为size(w) * len的子串单词计数与w相同,则找到一个答案。

Insertion Sort Integer Array Insertion Sort Linked List

Sort Integer Array using Insertion sort. //********************************************************************************************************/* Insertion Sort 原理:就是前面的sort部分全部是相对值,从后面拿一个元素,然后跟