leetcode-12-整数转罗马数字
生活随笔
收集整理的這篇文章主要介紹了
leetcode-12-整数转罗马数字
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
問題:
?
?
解:
package com.example.demo;public class Test12 {private static final int[] values = {1000, 900, 500, 400,100, 90, 50, 40,10, 9, 5, 4,1};private static final String[] symbols = {"M", "CM", "D", "CD","C", "XC", "L", "XL","X", "IX", "V", "IV","I"};/*** 將整數轉為羅馬數字* 定義兩個數組,一個數組列出所有可能出現的羅馬數字的情況,* 另一個列出其對應的整數** @param num* @return*/public String intToRoman(int num) {StringBuffer sb = new StringBuffer();int index = 0;while (num > 0) {int count = num / values[index];for (int i = 0; i < count; i++) {sb.append(symbols[index]);// 將計算過的值-去(將算過的值去掉)num -= values[index];}index++;}return sb.toString();}public static void main(String[] args) {Test12 t = new Test12();String s = t.intToRoman(1994);System.out.println(s);} }?
總結
以上是生活随笔為你收集整理的leetcode-12-整数转罗马数字的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: leetcode-11-盛最多水的容器
- 下一篇: leetcode-13-罗马数字转整数