鸿 网 互 联 www.68idc.cn

当前位置 : 服务器租用 > 编程语言开发 > java > >

【LeeCode】12. 整数转罗马数字

来源:互联网 作者:佚名 时间:2022-12-25 11:17
【题目描述】 罗马数字包含以下七种字符: ??I?? , ??V?? , ??X?? , ??L?? , ??C?? , ??D?? 和 ??M?? 。 【示例】 【代码】??liweiwei1419?? impor

【题目描述】

罗马数字包含以下七种字符: ??I??, ??V??, ??X??, ??L????C????D?? 和 ??M??

【示例】

【LeeCode】12. 整数转罗马数字_数组

【代码】??liweiwei1419??

import java.awt.image.ImageProducer;import java.util.*;import java.util.stream.Collectors;// 2022-12-19class Solution { public String intToRoman(int num) { String res = ""; // 把阿拉伯数字与罗马数字可能出现的所有情况和对应关系,放在两个数组中,并且按照阿拉伯数字的大小降序排列 int[] nums = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; String[] romans = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}; StringBuilder sb = new StringBuilder(); int index = 0; while (index < 13) { while (num >= nums[index]) { sb.append(romans[index]); num -= nums[index]; } index++; } res = sb.toString(); return res; }}public class Main{ public static void main(String[] args) { int arr = 58; new Solution().intToRoman(arr); // 输出: LVIII }}

【代码】官方

【LeeCode】12. 整数转罗马数字_罗马数字_02

上一篇:将Spring Cloud Gateway 与OAuth2模式一起使用
下一篇:没有了
网友评论
<