网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

leetCode12.IntegertoRoman|字符串|Medium

12. Integer to Roman

我们提供的服务有:成都网站制作、成都网站建设、外贸营销网站建设、微信公众号开发、网站优化、网站认证、温宿ssl等。为数千家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的温宿网站制作公司

Given an integer, convert it to a roman numeral.

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

题目大意:

将一个给定的阿拉伯数字转换成罗马数字。

思路:

这题看到的时候,想的太多。

其实很简单,将千位,百位,十位,个位都表示出来,然后组合即可。

代码如下:

class Solution {
public:
    string intToRoman(int num) {
        string thousands[4] = {"","M","MM","MMM"};
        string hundreds[10] = {"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"};
        string tens[10] = {"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"};
        string units[10] = {"","I","II","III","IV","V","VI","VII","VIII","IX"};

        string * hits[4] = {units,tens,hundreds,thousands};
        
        string result;
        int index = 0;
        while (num > 0)
        {
            result = hits[index][num % 10] + result;
            num = num / 10;
            index++;
        }
        
        return result;
    }
};

总结:

有时候题目没有那么难,不要自己搞的很复杂。问题简单化。简单化。。。

2016-08-19 15:16:29


分享文章:leetCode12.IntegertoRoman|字符串|Medium
文章起源:http://bjjierui.cn/article/ipdpch.html

其他资讯