[LeetCode 273] Integer to English Word

Java实现 LeetCode 273 整数转换英文表示 273. 整数转换英文表示 将非负整数转换为其对应的英文表示。可以保证给定输入小于 231 – 1 。 示例 1: 输入: 123 输出: “One Hundred Twenty Three” 示例 2: 输入: 12345 输出: “Twelve Thousand Three Hundred Forty Five” 示例 3: 输入: 1234567 输出: “One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven” 示例 4: 输入: 1234567891 输出: “One Billion Two Hund 立即下载

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)
solution:

Just as hint described, handle 3digit at each time, convert them into English word,

every time handle 3 digit, the unit will increase.

So many corner cases need to handle.

public String numberToWords(int num) {
        String[] units = {""," Thousand"," Million"," Billion"};
        int i = 0;
        String res="";
        while(num>0) {
            int temp = num%1000;
            if(temp>0) res = convert(temp) + units[i] + (res.length()==0 ?"": " "+res);
            num /= 1000;
            i++;
        }
        return res.isEmpty()? "Zero" : res;
    }
    public String convert(int num){
        String res = "";
        String[] ten = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
        String[] hundred = {"Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
        String[] twenty = {"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
        if(num>0) {
            int temp = num/100;
            if(temp>0) {
                res += ten[temp] + " Hundred";
            }
            temp = num%100;
            if(temp>=10 && temp<20){
                if(!res.isEmpty()) res +=" ";
                res = res + twenty[temp%10];
                return res;
            }else if(temp>=20){
                temp = temp/10;
                if(!res.isEmpty()) res +=" ";
                res = res + hundred[temp-1];
            }
            temp = num%10;
            if(temp>0) {
                if(!res.isEmpty()) res +=" ";
                res = res + ten[temp];
            }
        }
        return res;
    }

 



LC与RC滤波电路实战对比:从原理到选型指南 本文深入对比了LC与RC滤波电路的核心原理与实战应用。LC滤波利用电感与电容的无损能量搬运,适合高频、大电流电源滤波,效率高但需注意谐振风险;RC滤波通过电阻消耗能量实现滤波,结构简单、成本低,适用于低频信号处理或小电流供电场景。文章提供了从工作原理、性能维度到具体选型的完整指南,帮助工程师根据项目需求(如频率范围、负载电流、效率)做出精准选择。 阅读详情

相关推荐

在虚拟机上搭建FTP服务器

chroot_list_file=/etc/vsftpd/chroot_list # 对chroot_list下用户限制。[root@localhost ~]# vim /etc/vsftpd/vsftpd.conf #打开配置文件。# 指定用户的shell: -s /usr/bin/nologin。# 在配置文件中找到以下几行代码, 去掉注释(去掉 "#"号)# 指定用户主目录:/data/ftp/ftpuser -M。(2)修改配置文件,路径/etc/vsftpd/ 下的文件。

weixin_70753380的博客 3209

LeetCode每日一题之LeetCode273_整数转换英文表示

LeetCode273. 整数转换英文表示 LeetCode273 将非负整数 num 转换为其对应的英文表示。 示例 1: 输入:num = 123 输出:"One Hundred Twenty Three" 示例 2: 输入:num = 12345 输出:"Twelve Thousand Three Hundred Forty Five" 示例 3: 输入:num = 1234567 输出:"One Million Two Hundred Thirty Four Thousand Five Hund

风小猪的博客 608

【深度学习】普通卷积、DWconv(逐深度卷积)与PWconv(逐点卷积)以及DSconv(深度可分离卷积)

深度可分离卷积(Depthwise Separable Convolution)通过将常规卷积分解为逐通道卷积(Depthwise)和逐点卷积(Pointwise)两步,显著减少了参数量和计算成本。常规卷积同时提取空间和通道特征,而深度可分离卷积先对每个通道单独进行空间卷积,再用1×1卷积调整通道数。以输入3通道、输出4通道的64×64图像为例,常规卷积需108个参数,而深度可分离卷积仅需39个参数,减少了约2/3。这种结构在轻量级网络(如MobileNet)中广泛应用,可在保持性能的同时降低计算资源需求。

猫天意的博客 4060

LeetCode 第 273 场周赛题解及思路

LeetCode 第 273 场周赛题解及思路 2119. 反转两次的数字 2120. 执行所有后缀指令 2121. 相同元素的间隔之和 2122. 还原原数组

weixin_46323890的博客 625

2026年软考信息安全工程师备考指南

信息安全工程师是信息产业部和人事部举办的软考中新增开的一门考试。软考全称全国计算机技术与软件专业技术资格(水平)考试,这门新开的信息安全工程师分属该考试“信息系统”专业,位处中级资格,是信息产业部和人事部在最新的“国人厅发[2007]139号文件中新增的专业。2016年下半年,第一次开考信息安全工程师(中级)考试。 考试科目: 1.信息安全基础知识,考试时间为150分钟,笔试,选择题; ...

鹿鸣天涯 7396

VTK 显示大量点云数据及交互(点云拾取、着色、测量等)功能

VTK (Visualization Toolkit) 是一个强大的开源可视化库,非常适合处理点云数据。下面将介绍如何使用 VTK 显示大量点云数据,并实现点云拾取、着色、测量等功能。

深之JohnChen的专栏 1994

LeetCode:273. 整数转换英文表示————困难

题目 273. 整数转换英文表示 将非负整数 num 转换为其对应的英文表示。 示例 1: 输入:num = 123 输出:"One Hundred Twenty Three" 示例 2: 输入:num = 12345 输出:"Twelve Thousand Three Hundred Forty Five" 示例 3: 输入:num = 1234567 输出:"One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"

Kinght_123的博客 511

基于TKVA与深度学习的量子点微型光谱仪光谱重建改进算法研究

本文提出了一种融合深度学习的改进TKVA算法(TKVA-DL),用于提升量子点微型光谱仪的光谱重建精度。该方法创新性地结合深度先验网络与注意力机制,通过自适应学习光谱隐式先验替代传统固定正则化项,并动态加权探测器响应通道。实验表明,所提方法在仿真和实测数据上均显著优于传统TKVA及主流深度学习算法,RMSE降低35%,PSNR提升4.2 dB,同时保持物理可解释性。完整MATLAB实现验证了方法的有效性与可复现性,为微型光谱仪的高精度重建提供了新思路。 关键词:量子点微型光谱仪;光谱重建;Tikhonov正

AI的内部世界的博客 83

LeetCode 541. Reverse String II【字符串】简单

Given a string and an integer k , you need to reverse the first k characters for every 2k characters counting from the start of the string. If there are less than k characters left, reverse all of them. If there are less than 2k but greater than or equal

memcpy0的博客 631

如何通过单步调试的方法,找到 SAP Gateway 到底把 Fiori 应用发送的 OData 请求,投递到哪一台 ABAP 后台服务器了?

这是一个 Count 请求,得到服务器响应的数据是 204,这个 204 会以数字的形式,显示在 Fiori Launchpad tile 上。那如果当前 Gateway 系统可以连接到多台 ABAP 后台服务器,比如下图所示,可以连接 SAP S/4HANA,ERP 或者 SAP CRM,我怎么知道 SAP UI5 应用上显示的数据,到底来自哪一台 ABAP 后台服务器呢?,就是上图 ABAP 调试器里断点停留的位置,即 SAP Frontend 服务器,通过 RFC 将 OData 请求投递给。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 640

数学与纳米技术:微观世界的数学描述

《数学与纳米技术:微观世界的数学描述》 关键词:纳米技术、数学模型、微观世界、物理现象、计算模拟 摘要:本文探讨了数学在纳米技术领域的应用,从数学基础回顾、纳米技术引论,到物理现象与

AI天才研究院 1531

LeetCode刷题offer17-简单-打印从1到最大的n位数

算法作为极其重要的一点,是大学生毕业找工作的核心竞争力,所以为了不落后与人,开始刷力扣算法题!

布小禅的博客 5450

多思计组实验实验六 微程序控制器实验

实验六 微程序控制器实验

qq_53089329的博客 3783

LeetCode 273. Integer to English Words

273. Integer to English Words Hard Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1. Example 1: Input: 123 Output: “One Hundred Twe...

da_kao_la的博客 294

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

[leetcode] 273. Integer to English Words

Description Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1. Example 1: Input: 123 Output: "One Hundred Twenty Three" Example 2: ...

Learning from the mistakes 314

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

[leetcode-273]Integer to English Words(java)

问题描述: 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 T

中华胡杨的专栏 1098

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

LeetCode273. Integer to English Words

题目链接: https://leetcode.com/problems/integer-to-english-words/ 题目描述:将整数转化成英语单词字符串For example, 123 -> “One Hundred Twenty Three” 12345 -> “Twelve Thousand Three Hundred Forty Five” 1234567 -> “One M

_今已亭亭如盖矣 623

Leetcode 273. 整数转换英文表示 C++

Leetcode 273. 整数转换英文表示 题目 将非负整数转换为其对应的英文表示。可以保证给定输入小于 231 - 1 。 测试样例 示例 1: 输入: 123 输出: "One Hundred Twenty Three" 示例 2: 输入: 12345 输出: "Twelve Thousand Three Hundred Forty Five" 示例 3: 输入: 1234567 输出: "One Million Two Hundred Thirty Four Thousand Five Hund

weixin_43750513的博客 446

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

转载请注明出处:http://blog.youkuaiyun.com/crazy1235/article/details/51820937Subject 出处:https://leetcode.com/problems/integer-to-english-words/ Convert a non-negative integer to its english words representation.

crazy_jack 4139

[leetcode] 273.Integer to English Words

题目: Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 2^31 - 1.For example, 123 -> “One Hundred Twenty Three” 12345 -> “Twelve Thousand Th

u014673347的专栏 1264

LeetCode273——整数转换英文表示

我的LeetCode代码仓:https://github.com/617076674/LeetCode 原题链接:https://leetcode-cn.com/problems/integer-to-english-words/ 题目描述: 知识点:数学 思路:分区间递归求解 (1)首先,按千来划分,[0, 999],[1000, 999999],[1000000, 9999999...

清风阁 794

LeetCode第一刷--leetcode提交格式介绍与273. Integer to English Words

第一次玩Leetcode,对代码提交格式不了解,提交了一上午,略尴尬 leetcode确实有很多有意思的地方,不像以前做各个高校ACM练习,leetcode会给出错误信息(哪个数据的错了),也会提供测试用例自定义窗口,你可以自己设置测试样例 对于输出结果会有个比较窗口,出现你的程序和源程序结果比对,帮你快速定位错误原因 此外,leetcode只需要你写核心代码部分,读取输入函数,函数...

君只见独不见的专栏 1万+

【LeetCode】273. 整数转换英文表示 结题报告 (C++)

原题地址:https://leetcode-cn.com/problems/integer-to-english-words/ 题目描述: 将非负整数转换为其对应的英文表示。可以保证给定输入小于 231 - 1 。 示例 1: 输入: 123 输出: "One Hundred Twenty Three" 示例 2: 输入: 12345 输出: "Twelve Thousand Three...

暮雨凉初透 486

[Leetcode] 273. Integer to English Words

这一题真的只是烦而已。。。 这题的关键就是要看出英语算数字是每三个数一组的。 譬如说这个数:1,234,567。就是1 million 234 thousand 567这样。 再往下细谈,就是 "one" million "two hundred thirty four" thousand "five hundred sixty seven"  又或者说:1,234,567,890。就是1 ...

chaochen1407的专栏 320

[leetcode] Integer to English Words

1道模拟题花了2个多小时,我也是醉了。一方面由于好久没有刷题了,一方面这道题乱七八糟的细节特别多。于是就想简单总结下。 题意: 将整型数字(231-1以内)转化成英文单词,如: 思路很普通: 2^31 = 2 147 483 648, 我们可以将数字转化成字符串后,每3个字符分为1组,整体字符串无非就是每组数字构成的串+“Billion/Million/Thousand”之类; 可

曲径通幽处 1689

LeetCode273整数转英文

将非负整数转换为其对应的英文表示。可以保证给定输入小于 231 - 1 。 示例 1: 输入: 123 输出: "One Hundred Twenty Three" 示例 2: 输入: 12345 输出: "Twelve Thousand Three Hundred Forty Five" 示例 3: 输入: 1234567 输出: "One Million Two Hundr...

ysl的博客 524
上一篇: [LeetCode 221] Maximal Square
下一篇: [LeetCode 230] Kth Smallest Element in a BST
Mavs
博客等级 码龄17年 166粉丝 296原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值