当前位置:   article > 正文

字节跳动2-1 算法二轮面试202203-29_字节研发2-1

字节研发2-1

罗马数字包含以下七种字符: I, V, X, LCD 和 M

I             1
V             5
X             10
L             50
C             100
D             500
M             1000

这道题对应的是leetcode 中的12.整数转罗马数字

  1. package example;
  2. public class AlaboJiSuan {
  3. //组合数据左减右加
  4. String[] thousands = {"", "M", "MM", "MMM"};//1000,2000,3000
  5. String[] hundreds = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"}; //100,200,300,400,500,600,700,800.900
  6. String[] tens = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"}; //10,20,30,40,50,60,70,80,90
  7. String[] ones = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};//123456789
  8. public static void main(String[] args) {
  9. int remark = 11;
  10. AlaboJiSuan alaboJiSuan = new AlaboJiSuan();
  11. String numberStr = alaboJiSuan.getAlboNumberStr(remark);
  12. System.out.println(numberStr);
  13. System.out.println(alaboJiSuan.getAlboNumberStr(3999));
  14. }
  15. // 11
  16. // 11/%10
  17. private String getAlboNumberStr(int remark) {
  18. StringBuilder stringBuilder = new StringBuilder();
  19. stringBuilder.append(thousands[remark/1000]);
  20. stringBuilder.append(hundreds[remark%1000/100]);
  21. stringBuilder.append(tens[remark%100/10]);
  22. stringBuilder.append(ones[remark%10]);
  23. return stringBuilder.toString();
  24. }
  25. }

解决问题的思路其实是转换 0-9 转换成罗马数字的个十百千位数据。基本方法还是获取到每一位的数字。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/520766
推荐阅读
相关标签
  

闽ICP备14008679号