Leetcode

LeetCode #12 整数转罗马数字

2020-02-06  本文已影响0人  HU兔兔
class Solution {
public:
    string intToRoman(int num) {
         string Roma="IVXLCDM##";
         int i=6;
         int j=1000;
         int n,m;
         string ans;
         while(j){
             n=(num/j)%10;
             switch(n){
                 case 4:
                 ans.push_back(Roma[i]);
                 ans.push_back(Roma[i+1]);
                 break;
                 case 9:
                 ans.push_back(Roma[i]);
                 ans.push_back(Roma[i+2]);
                 break;
                 default:
                 if(n/5){
                     ans+=Roma[i+1];
                 }
                 for(m=1;m<=n%5;m++){
                     ans+=Roma[i];
                 }
             }
             j/=10;
             i-=2;
         }
         return ans;
    }
};

leetcode的计时是真滴有毒

上一篇下一篇

猜你喜欢

热点阅读