本文主要是介绍字节跳动2-1 算法二轮面试202203-29,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
罗马数字包含以下七种字符: I
, V
, X
, L
,C
,D
和 M
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
这道题对应的是leetcode 中的12.整数转罗马数字
package example;public class AlaboJiSuan {//组合数据左减右加String[] thousands = {"", "M", "MM", "MMM"};//1000,2000,3000String[] hundreds = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"}; //100,200,300,400,500,600,700,800.900String[] tens = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"}; //10,20,30,40,50,60,70,80,90String[] ones = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};//123456789public static void main(String[] args) {int remark = 11;AlaboJiSuan alaboJiSuan = new AlaboJiSuan();String numberStr = alaboJiSuan.getAlboNumberStr(remark);System.out.println(numberStr);System.out.println(alaboJiSuan.getAlboNumberStr(3999));}// 11// 11/%10private String getAlboNumberStr(int remark) {StringBuilder stringBuilder = new StringBuilder();stringBuilder.append(thousands[remark/1000]);stringBuilder.append(hundreds[remark%1000/100]);stringBuilder.append(tens[remark%100/10]);stringBuilder.append(ones[remark%10]);return stringBuilder.toString();}
}
解决问题的思路其实是转换 0-9 转换成罗马数字的个十百千位数据。基本方法还是获取到每一位的数字。
这篇关于字节跳动2-1 算法二轮面试202203-29的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!