码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode #Integer to Roman#

时间:2015-04-10 17:51:21      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:leetcode   c++   python   



技术分享


有点意思~ 只要相应的处理好对于 4 和 9 这两种特殊情况就好了.其他都是简单的加法


好吧, 老老实实贴出我的解答, 好丑... 不够简洁.. 可以看皓神的解答...



Python:

"""
Programmer  :   EOF
Date        :   2015.04.10
File        :   itr.py
E-mail      :   jasonleaster@gmail.com
"""

"""
Varible Description:

    @ret_string :   We put the returning string into this varible
    @base       :   Here is the base number for Roman counting process
    @table      :   We could use the base number to index the corresponding
                    representation in Roman Numbers
    @base_len   :   The length of @base
    @counter    :   A copy of @base_len
    @index      :   The current base number.

"""
class Solution:

    def intToRoman(self, num):

        ret_string = "" 

        base  = [1, 5, 10, 50, 100, 500, 1000]

        table = {1 :"I", 5  :"V", 10 :"X",                 50:"L", 100:"C", 500:"D", 1000:"M"}

        base_len = len(base)
        counter  = base_len
        while counter > 0:
            
            index = base[counter-1]
            tmp = num / index

            while tmp > 0:
                if self.hight_bit(num) == 9:
                    counter -= 1
                    index = base[counter - 1]
                    ret_string += table[ base[counter - 1]    ] +                                  table[ base[counter + 1]]
                    break;

                elif self.hight_bit(num) == 4:
                    index = base[counter - 1]
                    ret_string += table[ base[counter - 1]    ] +                                  table[ base[counter    ]    ]
                    break;

                else:
                    ret_string += table[index]
                    tmp -= 1;

            num  %= index
            counter -= 1

        return ret_string

    # We use this function to get the hightest bit in num conveniently :)
    def hight_bit(self, num):
        tmp = num
        while tmp > 10:
            tmp /= 10

        return  tmp
            

#----------- just for testing -----------------

s = Solution()

number = 4
print s.intToRoman(number)

number = 18
print s.intToRoman(number)

number = 3999
print s.intToRoman(number)

number = 1600
print s.intToRoman(number)

number = 321
print s.intToRoman(number)

number = 400
print s.intToRoman(number)




皓神的C++实现:

不过皓神的base元素比我的多些...原来多加几个base就可以简化问题...我伙呆


// Source : https://oj.leetcode.com/problems/integer-to-roman/
// Author : Hao Chen
// Date   : 2014-07-17

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

#include <stdlib.h>
#include <string>
#include <iostream>
using namespace std;

//greeding algorithm
string intToRoman(int num) {
    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}; 
    string result;

    for(int i=0; num!=0; i++){
        while(num >= value[i]){
            num -= value[i];
            result+=symbol[i];
        }
    }

    return result;
}


int main(int argc, char** argv)
{
    int num = 1234;
    if (argc>0){
        num = atoi(argv[1]);
    }    

    cout << num << " : " << intToRoman(num) << endl;
    return 0;
}







技术分享

LeetCode #Integer to Roman#

标签:leetcode   c++   python   

原文地址:http://blog.csdn.net/cinmyheart/article/details/44982493

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!