【JAVA、C++】LeetCode 012 Integer to Roman
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的更多相关文章
- 【JAVA、C++】 LeetCode 008 String to Integer (atoi)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- 【JAVA、C++】LeetCode 007 Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 解题思路:将数字 ...
- 【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 ...
- 【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 ...
- 【JAVA、C++】LeetCode 003 Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, ...
- 【JAVA、C++】LeetCode 002 Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- 【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 ...
- 【JAVA、C++】LeetCode 022 Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 【JAVA、C++】LeetCode 020 Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
随机推荐
- myeclipse 配置weblogic
1.打开myeclipse,选择Window -> Preferences--->MyEclipse--->servers 2.点击servers---->weblogic-- ...
- javaScript基础练习题-下拉框制作(CSS)
http://www.imooc.com/video/155 慕课网的视频,直接上代码 <!DOCTYPE hmtl> <html> <head> <meta ...
- HOPE——懦怯囚禁人的灵魂,希望可以感受自由。强者自救,圣者渡人。
人世间最美好的,就是希望 人世间最美好的,就是友谊 祝福所有相信希望的人,因为每个充满希望的人,都如此美丽. <肖申克的救赎>中的经典台词 1.Hope is a good thing, ...
- (转)CSS3 @font-face
@font-face是CSS3中的一个模块,他主要是把自己定义的Web字体嵌入到你的网页中,随着@font-face模块的出现,我们在Web的开发中使用字体不怕只能使用Web安全字体,你们当中或许有许 ...
- python学习笔记之module && package
个人总结: import module,module就是文件名,导入那个python文件 import package,package就是一个文件夹,导入的文件夹下有一个__init__.py的文件, ...
- Linux中用st_mode判断文件类型
Linux中用st_mode判断文件类型 2012-12-11 12:41 14214人阅读 评论(4) 收藏 举报 分类: Linux(8) C/C++(20) 版权声明:本文为博主原创文章, ...
- linux下ping的C语言实现(转)
#include <stdio.h> #include <signal.h> #include <arpa/inet.h> #include <sys/typ ...
- font-family属性与字体对齐
css中的font-family属性可以让我们自定义字体.在页面前端,宋体已经明日黄花,号称最贵中文字体的微软雅黑大行其道.英文字体万年不变,依然还是"arial","v ...
- nginx-upload-module模块实现文件断点续传
导读 每当我们想简单的实现文件上传功能,而又不使用其他的语言(比如PHP.Java),或者想实现文件的断点续传.这个时候Nginx的一个模块nginx-upload-module就能满足我们的需求. ...
- Docker大行其道—初识
导读 随着分布式.云计算.大数据的火热爆发,大量的云计算集群出现,光凭计算机硬件配置的已经无法再次一较高下,虚拟化成为其中最核心的技术.虚拟化既可以通过硬件模拟,也可以通过操作系统层面去实现,近年来热 ...