Given an integer, convert it to a roman numeral.

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

本题思路比较简单,难度主要集中在罗马数字本身,直接贴代码。

思路一,从高位开始:

JAVA:

	static public String intToRoman(int number) {
int[] values = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };
String[] numerals = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X","IX", "V", "IV", "I" };
//不推荐用String类型,因为+的本质是建立StringBuilder()的过程
StringBuilder result = new StringBuilder();
for (int i = 0; i < values.length; i++) {
while (number >= values[i]) {
number -= values[i];
result.append(numerals[i]);
}
}
return new String(result);
}

C++:

 class Solution {
public:
string intToRoman(int num) {
vector<int> values = { , , , , , , , , , , , , };
string numerals[] = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X","IX", "V", "IV","I" };
string res;
for (int i = ; i < values.size(); i++)
while (num >= values[i]) {
num -= values[i];
res+=numerals[i];
}
return res;
}
};

思路二,从低位开始:

JAVA:

static public String intToRoman(int num) {
String Roman[][] = {
{"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"},
{"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"},
{"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"},
{"", "M", "MM", "MMM"}
};
StringBuilder result = new StringBuilder();
for(int i=0;i<Roman.length;i++,num/=10)
result.insert(0,Roman[i][num % 10]);
return new String(result);
}

【JAVA、C++】LeetCode 012 Integer to Roman的更多相关文章

  1. 【JAVA、C++】 LeetCode 008 String to Integer (atoi)

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  2. 【JAVA、C++】LeetCode 007 Reverse Integer

    Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 解题思路:将数字 ...

  3. 【JAVA、C++】LeetCode 013 Roman to Integer

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

  4. 【JAVA、C++】LeetCode 005 Longest Palindromic Substring

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  5. 【JAVA、C++】LeetCode 003 Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  6. 【JAVA、C++】LeetCode 002 Add Two Numbers

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  7. 【JAVA、C++】LeetCode 001 Two Sum

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...

  8. 【JAVA、C++】LeetCode 022 Generate Parentheses

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  9. 【JAVA、C++】LeetCode 020 Valid Parentheses

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

随机推荐

  1. c#创建ISS站点

    private void CreateWebSite() { try { string installPath = "C:\\Program Files\\MyWeb"; stri ...

  2. getHibernateTemplate()的用法

    getHibernateTemplate() spring 中获得由spring所配置的hibernate的操作对象,然后利用此对象进行,保存,修改和删除等操作,此方法是在配置了spring以后,hi ...

  3. 最大ASCII的和问题

    问题:One day when you are going to clear all your browsing history, you come up with an idea: You want ...

  4. 【BZOJ-2818】Gcd 线性筛

    2818: Gcd Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 3347  Solved: 1479[Submit][Status][Discuss ...

  5. 【poj3714】 Raid

    http://poj.org/problem?id=3714 (题目链接) 现在才搞平面最近点对..感觉有点尴尬 题意 给出平面上两组点,每组n个,求两组点之间最短距离 Solution1 平面最近点 ...

  6. Erlang练习题----shopping

    直接就上代码了: -module(shop). -export([cost/1,total/1]). cost(orange) -> 5; cost(newspaper) -> 8; co ...

  7. 转:Linux集群-----HA浅谈

    通过特殊的软件将若干服务器连接在一起并提供故障切换功能的实体我们称之为高可用集群.可用性是指系统的uptime,在7x24x365的工作环境中,99%的可用性指在一年中可以有87小时36分钟的DOWN ...

  8. Initialization of deep networks

    Initialization of deep networks 24 Feb 2015Gustav Larsson As we all know, the solution to a non-conv ...

  9. PHP中is_numeric函数十六进制绕过0day

    0×00 简介国内一部分CMS程序里面有用到过is_numberic函数,我们先看看这个函数的结构bool is_numeric ( mixed $var )如果 var 是数字和数字字符串则返回 T ...

  10. SQL触发器

    1. 创建一个触发器,当一本书被还回时,从LOAN表中删除相应的借阅记录,将该学生借阅这本书记录添加到LoadHist表中:并检查是否有用户在等待预约这本书,如有则将这本书的借阅状况修改为 已经预约: ...