zoukankan      html  css  js  c++  java
  • leetcode -- Integer to Roman

    Given an integer, convert it to a roman numeral.

    Input is guaranteed to be within the range from 1 to 3999.

     1 public String intToRoman(int num) {
     2         // Start typing your Java solution below
     3         // DO NOT write main() function
     4         StringBuffer result = new StringBuffer();
     5         String[][] mapping = {
     6                 {"", "M", "MM", "MMM", "", "", "", "", "", ""}, //0 – 3000
     7                 {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"}, //100 – 900
     8                 {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"}, //10 – 90
     9                 {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"} //0 – 9    
    10         };
    11         int scale = 1000;
    12         int i = 0;
    13         while(num > 0){
    14             int digit = num / scale;
    15             result.append(mapping[i++][digit]);
    16             num = num % scale;
    17             scale /= 10;
    18         }
    19         return result.toString();
    20     }
  • 相关阅读:
    完数
    自定义的allocator
    成绩的处理
    R语言-线性回归(1)
    R语言-朴素贝叶斯分类器(1)
    R语言控制流
    leetcode Two sum
    数据库环境搭建
    表单验证制作注册页面
    表单验证
  • 原文地址:https://www.cnblogs.com/feiling/p/3302242.html
Copyright © 2011-2022 走看看