【LeetCode】Integer to Roman (2 solutions)

Integer to Roman

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 ret;
        //M<-->1000
        while(num >= 1000)
        {
            ret += ‘M‘;
            num -= 1000;
        }
        //to here, num < 1000
        //CM<-->900
        if(num >= 900)
        {
            ret += "CM";
            num -= 900;
        }
        //to here, num < 900
        //D<-->500
        if(num >= 500)
        {
            ret += ‘D‘;
            num -= 500;
        }
        //to here, num < 500
        if(num >= 400)
        {
            ret += "CD";
            num -= 400;
        }
        //to here, num < 400
        //C<-->100
        while(num >= 100)
        {
            ret += ‘C‘;
            num -= 100;
        }
        //to here, num < 100
        //XC<-->90
        if(num >= 90)
        {
            ret += "XC";
            num -= 90;
        }
        //to here, num < 90
        //L<-->50
        if(num >= 50)
        {
            ret += ‘L‘;
            num -= 50;
        }
        //to here, num < 50
        //XL<-->40
        if(num >= 40)
        {
            ret += "XL";
            num -= 40;
        }
        //to here, num < 40
        //X<-->10
        while(num >= 10)
        {
            ret += ‘X‘;
            num -= 10;
        }
        //to here, num < 10
        //IX<-->9
        if(num >= 9)
        {
            ret += "IX";
            num -= 9;
        }
        //to here, num < 9
        //V<-->5
        if(num >= 5)
        {
            ret += ‘V‘;
            num -= 5;
        }
        //to here, num < 5
        //IV<-->4
        if(num >= 4)
        {
            ret += "IV";
            num -= 4;
        }
        //to here, num < 4
        //I<-->1
        while(num >= 1)
        {
            ret += ‘I‘;
            num -= 1;
        }
        return ret;
    }
};

解法二:递归

class Solution {
public:
    string intToRoman(int num)
    {
        if(num>=1000)
            return "M"+intToRoman(num-1000);
        else if(num>=900)
            return "CM"+intToRoman(num-900);
        else if(num>=500)
            return "D"+intToRoman(num-500);
        else if(num>=400)
            return "CD"+intToRoman(num-400);
        else if(num>=100)
            return "C"+intToRoman(num-100);
        else if(num>=90)
            return "XC"+intToRoman(num-90);
        else if(num>=50)
            return "L"+intToRoman(num-50);
        else if(num>=40)
            return "XL"+intToRoman(num-40);
        else if(num>=10)
            return "X"+intToRoman(num-10);
        else if(num>=9)
            return "IX"+intToRoman(num-9);
        else if(num>=5)
            return "V"+intToRoman(num-5);
        else if(num>=4)
            return "IV"+intToRoman(num-4);
        else if(num>=1)
            return "I"+intToRoman(num-1);
        else
            return "";
    }
};

时间: 2024-10-02 17:35:42

【LeetCode】Integer to Roman (2 solutions)的相关文章

【LeetCode】Integer to Roman

Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. public class Solution { public String intToRoman(int num) { StringBuilder sb = new StringBuilder(); if(num==0) return sb.toString(); while(num

【LeetCode】Integer to Roman 和 Roman to Integer

[题目] Given a roman numeral, convert it to an integer. Or, Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. [罗马数字] 1~9: {"I", "II", "III", "IV", "V"

【LeetCode】Search Insert Position (2 solutions)

Search Insert Position Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. Here are few examples.[1,3,5,

【Leetcode】Integer Break

题目链接:https://leetcode.com/problems/integer-break/ 题目: Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get. For example, given n = 2, re

【LeetCode】338. Counting Bits (2 solutions)

Counting Bits Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array. Example:For num = 5 you should return [0,1,1,2,1,2]. Follow up

【LeetCode】Factorial Trailing Zeroes (2 solutions)

Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. Credits:Special thanks to @ts for adding this problem and creating all test cases. 对n!做质因数分解n!=2x*

【LeetCode】First Missing Positive (2 solutions)

First Missing Positive Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] return 3,and [3,4,-1,1] return 2. Your algorithm should run in O(n) time and uses constant space. 解法一:O(nlogn) time and O(1) sp

【LeetCode】Find Peak Element (3 solutions)

Find Peak Element A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ num[i+1], find a peak element and return its index. You may imagine that num[-1] = num[n] = -∞. For example, in array [1, 2, 3, 1],

【LeetCode】242. Valid Anagram (2 solutions)

Valid Anagram Given two strings s and t, write a function to determine if t is an anagram of s. For example,s = "anagram", t = "nagaram", return true.s = "rat", t = "car", return false. Note:You may assume the strin