LeetCode 12 整数转罗马数字
生活随笔
收集整理的這篇文章主要介紹了
LeetCode 12 整数转罗马数字
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
https://leetcode-cn.com/problems/integer-to-roman/
解決方案
class Solution {int[] values = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};String[] symbols = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};public String intToRoman(int num) {StringBuffer sb = new StringBuffer();for (int i = 0; i < values.length && num > 0; i++) {while (num >= values[i]) {sb.append(symbols[i]);num -= values[i];}}return sb.toString();} }總結
以上是生活随笔為你收集整理的LeetCode 12 整数转罗马数字的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LeetCode 11 盛最多水的容器
- 下一篇: LeetCode 13 罗马数字转整数