当前位置: 首页 > news >正文

LeetCode -- Integer to English Words

题目描述:


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"


就是把数字转化为英语单词。




思路:
1.先把从100-999映射为英文单词的函数写出来:
1.1先考虑一位数:一一映射就可以了。函数为Digit()
1.2然后考虑两位数的情况:需要区分11-19,函数为Teen()和20-99的情况,函数为Ty()
1.3然后对百位数调用Digit()就可以了。如果百位数为0,则调用Teen()或Ty()
函数存为Hundred()
2.考虑小于等于6位数的情况,即百万以下的数。
将这六位数分为前3位和后3位,分别调用Hundred()函数,中间插入"Thousand"即可。
函数存为Thousand()
3.考虑小于等于9位数的情况,同6位数处理过程类似,考虑前3位和后六位,分别调用Thousand(),中间插入"Million"即可
函数存为Million()
4.考虑9位数以上的情况,同理,考虑前3位和后9位,分别调用Million()函数,中间插入"Billion"
5.需要注意的是,所有输出都是首字母大写


本题的难点在于情况特别多,边界值的情况尤其要注意。问题分类的话算是数学问题。


实现代码:


public class Solution {
    
    public string NumberToWords(int num) 
    {
        if(num == 0){
    		return "Zero";
    	}
	
    	var str = num.ToString();
    	
    	if(num > 999999999){
    		var s1 = str.Substring(0,str.Length - 9);
    		var n1 = int.Parse(s1);
    		var s2 = str.Substring(str.Length - 9, 9);
    		
    		var s = Millions(s1) + " Billion " + Millions(s2);
    		s = s.TrimEnd();
    		return s;
    	}
    	else{
    		return Millions(str);
    	}
    }
    
    private string Millions(string str)
    {
    	var n = int.Parse(str);
    	if(n > 999999){
    		var s1 = str.Substring(0,str.Length - 6);
    		var n1 = int.Parse(s1);
    		var s2 = str.Substring(str.Length - 6, 6);
    		
    		var s = Thousands(s1) + " Million " + Thousands(s2);
    		s = s.TrimEnd();
    		return s;
    	}
    	else{
    		return Thousands(n.ToString());
    	}
    }
    
    
    private string Thousands(string str)
    {
    	var n = int.Parse(str);
    	if(n > 999){
    		var s1 = str.Substring(0,str.Length - 3);
    		var n1 = int.Parse(s1);
    		var s2 = str.Substring(str.Length - 3, 3);
    		
    		var s = Hundreds(s1)+ " Thousand " + Hundreds(s2);
    		s = s.TrimEnd();
    		return s;
    	}
    	else{
    		return Hundreds(n.ToString());
    	}
    }
    
    private string Hundreds(string str)
    {
    	int n = int.Parse(str);
    	if(n < 10){
    		return Digit(str.Last());
    	}
    	else if(n == 10){
    		return "Ten";
    	}
    	else if(n > 10 && n < 20)
    	{
    		return Teen(str.Substring(str.Length - 2,2));
    	}
    	else if(n >= 20 && n < 100)
    	{
    		return Ty(str.Substring(str.Length - 2,2));
    	}
    	else if(n >= 100 && n <= 999)
    	{
    		var c1 = str[0];
    		var s = Digit(c1) + " Hundred";
    		
    		var c2 = str[1];
    		var c3 = str[2];
    		if(c2 == '0'){
    			return c3 != '0' ? s + " " + Digit(c3) : s;	
    		}
    		if(c2 == '1'){
    			return s + " " + Teen(str.Substring(str.Length - 2,2));
    		}
    		else{
    			return s + " " + Ty(str.Substring(str.Length - 2,2));
    		}
    	}
    	else{
    		throw new ArgumentException("input should be less than 1000");
    	}
    }
    
    private string Teen(string str)
    {
    	switch(str){
    		case "10":
    		return "Ten";
    		case "11":
    		return "Eleven";
    		case "12":
    		return "Twelve";
    		case "13":
    		return "Thirteen";
    		case "14":
    		return "Fourteen";
    		case "15":
    		return "Fifteen";
    		case "16":
    		return "Sixteen";
    		case "17":
    		return "Seventeen";
    		case "18":
    		return "Eighteen";
    		case "19":
    		return "Nineteen";
    		default :
    		throw new ArgumentException("input should be from 11 to 19 ONLY.");
    	}
    }
    
    private string Ty(string str)
    {
    	var c1 = str[0];
    	var c2 = str[1];
    	var space = c2 == '0' ? "" : " ";
    	switch(c1){
    		case '2':
    		return "Twenty" +space+ Digit(c2);
    		case '3':
    		return "Thirty" +space+ Digit(c2);
    		case '4':
    		return "Forty" +space+ Digit(c2);
    		case '5':
    		return "Fifty" +space+ Digit(c2);
    		case '6':
    		return "Sixty" +space+ Digit(c2);
    		case '7':
    		return "Seventy" +space+ Digit(c2);
    		case '8':
    		return "Eighty" +space+ Digit(c2);
    		case '9':
    		return "Ninety" +space+ Digit(c2);
    		default :
    		throw new ArgumentException(string.Format("input should between [20,99], but got {0}",c1));
    	}
    }
    
    private string Digit(char c)
    {
    	switch(c){
    		case '0':
    		return "";
    		case '1':
    		return "One";
    		case '2':
    		return "Two";
    		case '3':
    		return "Three";
    		case '4':
    		return "Four";
    		case '5':
    		return "Five";
    		case '6':
    		return "Six";
    		case '7':
    		return "Seven";
    		case '8':
    		return "Eight";
    		case '9':
    		return "Nine";
    		default :
    		throw new ArgumentException("input should beteen [0-9]");
    	}
    }


}


相关文章:

  • WiMAX组网技术与解决方案
  • LeetCode -- Sum Root to Leaf Numbers
  • 移动设备管理(MDM)与OMA(OTA)DM协议向导(三)——AAA服务器
  • LeetCode -- Surrounded Regions
  • LeetCode -- Triangle
  • Nebula3中的骨骼动画: Animation子系统
  • LeetCode -- Ugly Number II
  • LeetCode -- Ugly Number
  • vim 显示行号、语法高亮、自动缩进的设置
  • LeetCode -- Linked List cycle
  • 根据textbox中的值,改变dropdownlist的选项
  • LeetCode -- Basic Calculator II
  • 完整SQL分页存储过程(支持多表联接)
  • LeetCode -- Bitwise AND of Numbers Range
  • C 符号列表
  • @jsonView过滤属性
  • 【跃迁之路】【444天】程序员高效学习方法论探索系列(实验阶段201-2018.04.25)...
  • Idea+maven+scala构建包并在spark on yarn 运行
  • Java 多线程编程之:notify 和 wait 用法
  • java 多线程基础, 我觉得还是有必要看看的
  • JavaScript学习总结——原型
  • java概述
  • Laravel Telescope:优雅的应用调试工具
  • puppeteer stop redirect 的正确姿势及 net::ERR_FAILED 的解决
  • TypeScript迭代器
  • weex踩坑之旅第一弹 ~ 搭建具有入口文件的weex脚手架
  • 给github项目添加CI badge
  • 开发基于以太坊智能合约的DApp
  • 前端 CSS : 5# 纯 CSS 实现24小时超市
  • 前言-如何学习区块链
  • 深入浏览器事件循环的本质
  • 十年未变!安全,谁之责?(下)
  • 使用阿里云发布分布式网站,开发时候应该注意什么?
  • 双管齐下,VMware的容器新战略
  • 我这样减少了26.5M Java内存!
  • 用quicker-worker.js轻松跑一个大数据遍历
  • 阿里云ACE认证之理解CDN技术
  • 关于Kubernetes Dashboard漏洞CVE-2018-18264的修复公告
  • (22)C#传智:复习,多态虚方法抽象类接口,静态类,String与StringBuilder,集合泛型List与Dictionary,文件类,结构与类的区别
  • (八十八)VFL语言初步 - 实现布局
  • (补)B+树一些思想
  • (二)正点原子I.MX6ULL u-boot移植
  • (附表设计)不是我吹!超级全面的权限系统设计方案面世了
  • (简单有案例)前端实现主题切换、动态换肤的两种简单方式
  • (教学思路 C#之类三)方法参数类型(ref、out、parmas)
  • (十八)devops持续集成开发——使用docker安装部署jenkins流水线服务
  • (十五)Flask覆写wsgi_app函数实现自定义中间件
  • (算法)前K大的和
  • (学习日记)2024.03.25:UCOSIII第二十二节:系统启动流程详解
  • *setTimeout实现text输入在用户停顿时才调用事件!*
  • ../depcomp: line 571: exec: g++: not found
  • .NET Core日志内容详解,详解不同日志级别的区别和有关日志记录的实用工具和第三方库详解与示例
  • .NET Core使用NPOI导出复杂,美观的Excel详解
  • .NET Standard / dotnet-core / net472 —— .NET 究竟应该如何大小写?
  • .NET 分布式技术比较