每日算法之十一:Integer to Roman

题目: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;

其中每两个阶段的之间有一个减法的表示,比如900=CM, C写在M前面表示M-C。

范围给到3999,感觉情况不多直接打表其实更快,用代码判断表示估计比较繁琐。

然后就是贪心的做法,每次选择能表示的最大值,把对应的字符串连起来。

穷举的方法:

class Solution {
public:
    string intToRoman(int num) {
        string roma = "";
        if(num/1000)
        {
            switch(num/1000)
            {
                case 1:
                    roma = roma+"M";
                    break;
                case 2:
                    roma = roma+"MM";
                    break;
                case 3:
                    roma = roma+"MMM";
                    break;
            }
        }
        num = num%1000;
        if(num/100)
        {
            switch(num/100)
            {
                case 1:roma = roma+"C";break;
                case 2:roma = roma+"CC";break;
                case 3:roma = roma+"CCC";break;
                case 4:roma = roma+"CD";break;
                case 5:roma = roma+"D";break;
                case 6:roma = roma+"DC";break;
                case 7:roma = roma+"DCC";break;
                case 8:roma = roma+"DCCC";break;
                case 9:roma = roma+"CM";break;
            }
        }
        num = num%100;
        if(num/10)
        {
            switch(num/10)
            {
                case 1:roma = roma+"X";break;
                case 2:roma = roma+"XX";break;
                case 3:roma = roma+"XXX";break;
                case 4:roma = roma+"XL";break;
                case 5:roma = roma+"L";break;
                case 6:roma = roma+"LX";break;
                case 7:roma = roma+"LXX";break;
                case 8:roma = roma+"LXXX";break;
                case 9:roma = roma+"XC";break;
            }
        }
        num = num%10;
        switch(num)
        {
                case 1:roma = roma+"I";break;
                case 2:roma = roma+"II";break;
                case 3:roma = roma+"III";break;
                case 4:roma = roma+"IV";break;
                case 5:roma = roma+"V";break;
                case 6:roma = roma+"VI";break;
                case 7:roma = roma+"VII";break;
                case 8:roma = roma+"VIII";break;
                case 9:roma = roma+"IX";break;
        }
        return roma;
    }
};

遍历实现的方法:

class Solution {
public:
    string intToRoman(int num) {
        string str;
        string symbol[]={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
        int value[]=    {1000,900,500,400, 100, 90,  50, 40,  10, 9,   5,  4,   1};
        for(int i=0;num!=0;++i)
        {
            while(num>=value[i])
            {
                num-=value[i];
                str+=symbol[i];
            }
        }
        return str;
    }
};

每日算法之十一:Integer to Roman

时间: 2024-08-04 10:06:01

每日算法之十一:Integer to Roman的相关文章

每日算法之四十一:Spiral Matrix II (螺旋矩阵)

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example, Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 针对这个问题采用最直观的方式即可,即螺旋插入,这里有两个地方需要注意,一个是插入边界的界定,

每日算法之三十一:Combination Sum

给定一个整数序列,求解一个子序列,子序列之和等于给定目标值.子序列满足以下条件: 1)子序列是有序的 2)子序列的元素个数不限,可以是给定元素的重复元素. 3)结果中的子序列是唯一的 原题描述如下: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeat

每日算法之八:String to Integer (atoi) 及溢出分析

Implement atoi to convert a string to an integer.  Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.Notes: It is intended for this problem to be spe

经典算法题每日演练——第二十一题 十字链表

原文:经典算法题每日演练--第二十一题 十字链表 上一篇我们看了矩阵的顺序存储,这篇我们再看看一种链式存储方法“十字链表”,当然目的都是一样,压缩空间. 一:概念 既然要用链表节点来模拟矩阵中的非零元素,肯定需要如下5个元素(row,col,val,down,right),其中: row:矩阵中的行. col:矩阵中的列. val:矩阵中的值. right:指向右侧的一个非零元素. down:指向下侧的一个非零元素. 现在我们知道单个节点该如何表示了,那么矩阵中同行的非零元素的表示不就是一个单链

经典算法题每日演练——第十一题 Bitmap算法

原文:经典算法题每日演练--第十一题 Bitmap算法 在所有具有性能优化的数据结构中,我想大家使用最多的就是hash表,是的,在具有定位查找上具有O(1)的常量时间,多么的简洁优美, 但是在特定的场合下: ①:对10亿个不重复的整数进行排序. ②:找出10亿个数字中重复的数字. 当然我只有普通的服务器,就算2G的内存吧,在这种场景下,我们该如何更好的挑选数据结构和算法呢? 一:问题分析 这年头,大牛们写的排序算法也就那么几个,首先我们算下放在内存中要多少G: (10亿 * 32)/(1024*

LeetCode:Integer to Roman

1.题目名称 Integer to Roman (阿拉伯数字到罗马数字的转换) 2.题目地址 https://leetcode.com/problems/integer-to-roman 3.题目内容 英文:Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 中文:给出一个整数,将它转换成罗马数字.输入在1-3999之间. 4.题目分

每日算法之四十三:Rotate List (列表旋转k个元素)

Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given 1->2->3->4->5->NULL and k = 2, return 4->5->1->2->3->NULL. 这里的k可能是比链表长度要大的数字,因此实际旋转的位置就是k%len(list).如果这个计算结果等于零或者等于len(list),

每日算法之四十二:Permutation Sequence (顺序排列第k个序列)

The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the permutations in order, We get the following sequence (ie, for n = 3): "123" "132" "213" "231" "312" "

每日算法之四十:Insert Interval

Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Example 1: Given intervals [1,3],[6,9], insert and merge