字节跳动2-1 算法二轮面试202203-29
Posted 史上最强的弟子
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了字节跳动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,3000
String[] hundreds = "", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"; //100,200,300,400,500,600,700,800.900
String[] tens = "", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"; //10,20,30,40,50,60,70,80,90
String[] ones = "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX";//123456789
public 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/%10
private 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的主要内容,如果未能解决你的问题,请参考以下文章