用Rust刷leetcode第十二题

Problem

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

Symbol Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000

For example, two is written as II in Roman numeral, just two one’s added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II.

Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:

  • I can be placed before V (5) and X (10) to make 4 and 9.
  • X can be placed before L (50) and C (100) to make 40 and 90.
  • C can be placed before D (500) and M (1000) to make 400 and 900.

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

Example

Example 1:

Input: 3
Output: “III”

Example 2:

Input: 4
Output: “IV”

Example 3:

Input: 9
Output: “IX”

Example 4:

Input: 58
Output: “LVIII”
Explanation: L = 50, V = 5, III = 3.

Example 5:

Input: 1994
Output: “MCMXCIV”
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.

Solution

impl Solution {    
    pub fn int_to_roman(num: i32) -> String {
        let mut ret = String::from("");

        let mut temp = num;
        if temp >= 1000 {
            let ret1 = temp/1000;
            for i in 0..ret1 {
                ret.push('M');
            }
            temp -= 1000*ret1;
        }

        if temp >= 900 {
            ret.push('C');
            ret.push('M');
            temp = temp - 900;
        }

        if temp >= 500 {
            ret.push('D');
            temp -= 500;
        }

        if temp >= 400 {
            ret.push('C');
            ret.push('D');
            temp -= 400;
        }

        if temp >= 100 {
            let ret1 = temp/100;
            for i in 0..ret1 {
                ret.push('C');
            }
            temp -= ret1*100;
        }

        if temp >= 90 {
            ret.push('X');
            ret.push('C');
            temp = temp - 90;
        }


        if temp >= 50 {
            ret.push('L');
            temp -= 50;
        }

        if temp >= 40 {
            ret.push('X');
            ret.push('L');
            temp -= 40;
        }

        if temp >= 10 {
            let ret1 = temp/10;
            for i in 0..ret1 {
                ret.push('X');
            }
            temp -= ret1*10;
        }

        if temp == 9 {
            ret.push('I');
            ret.push('X');
            temp = 0;
        }

        if temp >= 5 {
            ret.push('V');
            temp -= 5;
        }

        if temp == 4 {
            ret.push('I');
            ret.push('V');
            temp = 0;
        }

        if temp > 0 {
            for i in 0..temp {
                ret.push('I');
            }
        }

        return ret;
    }
}
本作品采用《CC 协议》,转载必须注明作者和本文链接
令狐一冲
讨论数量: 2

我换个号来支持你了 :+1: 江哥

3年前 评论

感谢支持,顺便说一句,江沐涵也是笔名, :smile:

3年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!
文章
255
粉丝
119
喜欢
308
收藏
128
排名:335
访问:2.8 万
私信
所有博文
社区赞助商