题目链接:https://leetcode.com/problems/integer-to-roman/
解题思路:
遇到这种题,没有好的方法,记住他怎么做的。
首先,由于罗马符号都是累加上去的,没什么乘法操作,所以我们在数字转换的时候也是累加。
把所有可能出现的符号和数字都对应出来,然后用num不断去减最大的符号代表的数,每成功减一次,就把符号串起来。
1 class Solution { 2 public String intToRoman(int num) { 3 4 String str =""; 5 String [] symbol = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"}; 6 7 int [] value = {1000,900,500,400,100,90,50,40,10,9,5,4,1}; 8 9 for(int i=0;num!=0;i++) 10 { 11 while(num>=value[i]) 12 { 13 num = num-value[i]; 14 str = str +symbol[i]; 15 } 16 } 17 return str; 18 19 20 } 21 }
原文:https://www.cnblogs.com/wangyufeiaichiyu/p/10827766.html