一天一道LeetCode系列

(一)题目

Given an integer, convert it to a roman numeral.

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

(二)解题

将整形数字转换成罗马数字

罗马数字共有七个,即I(1),V(5),X(10),L(50),C(100),D(500),M(1000)

举例:Ⅰ、Ⅱ、Ⅲ、Ⅳ、Ⅴ、Ⅵ、Ⅶ、Ⅷ、Ⅸ、Ⅹ、Ⅺ、Ⅻ表示1-11

代码:

class Solution {
public:
    string intToRoman(int num) {
        string str = "";
        string roman[8] = {"I","V","X","L","C","D","M"};
        int j=0;
        while(num)
        {
            int temp = num%10;
            string str_t ="";
            if(temp <4)
            {
                int count = temp;
                while(count--) str_t+=roman[j];
            }
            else if(temp==4)
            {
                str_t=roman[j];
                str_t+=roman[j+1];
            }
            else if(temp>=5&&temp<=8)
            {
                str_t+=roman[j+1];
                int count = temp-5;
                while(count--) str_t+=roman[j];
            }
            else if(temp==9)
            {
                str_t+=roman[j];
                str_t+=roman[j+2];
            }
            cout<<roman[j]<<endl;
            cout<<str_t<<endl;
            str =str_t+str;
            j=j+2;
            num = num/10;
        }
        return str;
    }
};

我这段代码看起来比较臃肿。看到网上的写法如beiyeqingteng博主的:

public class Solution {
    public String intToRoman(int number) {
        int[] values = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };
        String[] numerals = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < values.length; i++) {
            while (number >= values[i]) {
                number -= values[i];
                result.append(numerals[i]);
            }
        }
        return result.toString();
    }
}  

看起来就简单多了。

【一天一道LeetCode】#12 Integer to Roman的更多相关文章

  1. Leetcode 12——Integer to Roman

    12.Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be withi ...

  2. Leetcode 12. Integer to Roman(打表,水)

    12. Integer to Roman Medium Roman numerals are represented by seven different symbols: I, V, X, L, C ...

  3. [LeetCode] 12. Integer to Roman 整数转化成罗马数字

    Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...

  4. [LeetCode] 12. Integer to Roman ☆☆

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

  5. [LeetCode] 12. Integer to Roman 整数转为罗马数字

    Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...

  6. Java [leetcode 12] Integer to Roman

    题目描述: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range fr ...

  7. [leetcode]12. Integer to Roman整数转罗马数字

    Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...

  8. LeetCode——12. Integer to Roman

    一.题目链接:https://leetcode.com/problems/integer-to-roman/ 二.题目大意: 给定一个整数,返回它的罗马数字的形式. 三.题解: 要想做出这道题目,首先 ...

  9. LeetCode 12 Integer to Roman (整数转罗马数字)

    题目链接: https://leetcode.com/problems/integer-to-roman/?tab=Description   String M[] = {"", ...

  10. [leetcode] 12. Integer to Roman

    关于罗马数字: I: 1V: 5X: 10L: 50C: 100D: 500M: 1000字母可以重复,但不超过三次,当需要超过三次时,用与下一位的组合表示:I: 1, II: 2, III: 3, ...

随机推荐

  1. HA机制下的Hadoop配置

    [版权申明:本文系作者原创,转载请注明出处] 文章出处:http://www.cnblogs.com/sdksdk0/p/5585355.html 作者: 朱培    ID:sdksdk0 ----- ...

  2. CountDownLatch使用

    分享牛原创,CountDownLatch类的使用,CountDownLatch是一个工具类,运行主线程开启子线程的时候,子线程还没有结束的时候,主线程可以一直等待,直到初始化的现成的计数器count为 ...

  3. GirlFriendNotFoundException异常是怎样处理的?

    GirlFriendNotFoundException异常是怎样处理的? 如果说要去创造这个异常,那么我们的JAVA程序里,肯定是继承Exception去处理,所有我们可以先实现一个自己的Except ...

  4. 初识gd库

    必备基础 开启GD拓展 列表使用 获取图片信息代码 图片详细信息 特效函数 示例 运行结果 分析 获取图片基本信息 获取图片宽度 获取图片高度 获取图片后缀名 获取图片mime类型 操作图片 添加文字 ...

  5. RxJava操作符(03-变换操作)

    转载请标明出处: http://blog.csdn.net/xmxkf/article/details/51649975 本文出自:[openXu的博客] 目录: Buffer FlatMap fla ...

  6. RxJava(六) retryWhen操作符实现错误重试机制

    欢迎转载,转载请标明出处: http://blog.csdn.net/johnny901114/article/details/51539708 本文出自:[余志强的博客] 业务需求 当我们在app里 ...

  7. Android学习路线指南

    看到这位大牛的博文,不禁得感概,我最近也遇到了很多问题,内心彷徨不堪,转载大牛这篇博文,是为了更好的勉励自己.原文地址在最后面. 前言 看到一篇文章中提到"最近几年国内的初级Android程 ...

  8. Android初级教程:屏幕分辨率

    在app编码中经常需要获取手机的屏幕分辨率(宽*高),原来我直接上网拷贝代码,但在使用过程中却发现诸多不便. 不便一:下面代码中的getWidth和getHeight在adt上提示deprecated ...

  9. 最简单的基于librtmp的示例:发布H.264(H.264通过RTMP发布)

    ===================================================== 最简单的基于libRTMP的示例系列文章列表: 最简单的基于librtmp的示例:接收(RT ...

  10. 开源项目——小Q聊天机器人V1.2

    小Q聊天机器人V1.0 http://blog.csdn.net/baiyuliang2013/article/details/51386281 小Q聊天机器人V1.1 http://blog.csd ...