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. 6.Android之switch和togglebutton按钮学习

    Switch和ToggleButton按钮在手机上也经常看到,比如手机设置里面wlan,蓝牙,gps开关等. 首先在工具上拖进一个switch和togglebutton开关按钮,如图 生成xml代码如 ...

  2. 重启猫(modem)的方法

    重启猫(modem)的方法 家里上网还是古老的"猫+路由器"模式,换路由器后就要reset猫,其步骤为: 断开猫电源 用针头或笔尖按住reset小孔,持续30秒 针抵住小孔的同时连 ...

  3. UVa 1347 Tour

    Tour Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Description   Joh ...

  4. DLUTOJ 1331 Maximum Sum

    传送门 Time Limit: 1 Sec  Memory Limit: 128 MB  Description You are given an array of size N and anothe ...

  5. Codeforces 567D One-Dimensional Battle Ships

    传送门 D. One-Dimensional Battle Ships time limit per test 1 second memory limit per test 256 megabytes ...

  6. route工具

    route工具 route工具主要用来查看或修改内核路由表. 1.查看内核路由表 route [-nee] 参数说明: -n:不要使用协议或主机名称,直接使用 IP 或 port number:-ee ...

  7. 七层负载均衡——HAProxy

    HAProxy入门 HAProxy是一款提供高可用性.负载均衡以及基于TCP(第四层)和HTTP(第七层)应用的代理软件,HAProxy是完全免费的.借助HAProxy可以快速并且可靠的提供基于TCP ...

  8. FreeMarker template error!

    部署项目后发现以下“FreeMarker template error!”的问题,google.baidu猛一顿搜索无果后开始认真分析异常信息. FreeMarker template error! ...

  9. Android SDK Manager和AVD Manager使用

    Android SDK Manager和AVD Manager使用(win7_64bit下测试) 目录 1.概述 2.本文用到的工具 3.安卓开发基础工具包下载 4.Android SDK Manag ...

  10. 第三方br查询工具害人不浅

    第三方br查询工具害人不浅,查询的时候会大批量调用百度的数据库,为什么说是大批量查询呢? 首先是自己查询,心急的站长恨不得下一次刷新br时数值会有所提高,不是那么急的也会一天查一次或几天一次,记录网站 ...