LeetCode: Interger to Roman

Given an integer, convert it to a roman numeral.

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

相对应的一道题:Roman to Interger : http://www.cnblogs.com/double-win/p/3760002.html


 1 class Solution {
2 public:
3 string intToRoman(int num) {
4 string s[]={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
5 int n[]={1000,900,500,400,100,90,50,40,10,9,5,4,1};
6 string ans;
7 int i=0;
8 while(num>0)
9 {
10 if(num>=n[i])
11 {
12 num-=n[i];
13 ans+= s[i];
14 }
15 else
16 i++;
17 }
18 return ans;
19 }
20 };

时间: 2025-01-18 10:12:36

LeetCode: Interger to Roman的相关文章

[leetcode] Integer to Roman @ Python

题目: https://oj.leetcode.com/problems/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: # @return a string def intToRoman(self, num): ints = [100

LeetCode解题报告--Roman to Integer

题目:罗马数字转为阿拉伯数字 Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 分析:题意:将给定的罗马数字转为阿拉伯数字 从前往后遍历罗马数字,如果某个数比前一个数小,则把该数加入到结果中: 反之,则在结果中两次减去前一个数并加上当前这个数: java 代码:(accepted) import java.util.HashMap;

[LeetCode]Integer to Roman

int型数字转化为罗马数字的形式 思路: 由于只是1到3999,一共只有四位,分别求这四位的情况. 可以将每一位从1到9,int和罗马数字的一一对应的关系给出来,然后直接转换. /***************************************************************** Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range fr

[LeetCode] NO.13 Roman to Integer

[题目] Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. [题目解析] 这道题很关键的是要理解罗马数字的几个主要的代表符号和表示方式,罗马数字共有七个,即I(1),V(5),X(10),L(50),C(100),D(500),M(1000). 用来表示数字的时候,如果相邻的两个罗马数字,前面表示的数字比后面的小,那么就要减去前面的

【LeetCode】6 - Roman to Integer

Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. Solution: 基本字符 I V X L C D M 相应的阿拉伯数字表示为 1 5 10 50 100 500 1000 个位数举例 Ⅰ-1.Ⅱ-2.Ⅲ-3.Ⅳ-4.Ⅴ-5.Ⅵ-6.Ⅶ-7.Ⅷ-8.Ⅸ-9 十位数举例 Ⅹ-10.Ⅺ-11.Ⅻ-12.XIII-13.XIV-14.

【leetcode】13. Roman to Integer

题目描述: Given a roman numeral, convert it to an integer. 解题思路: 罗马计数法有以下规则: 基本数字 Ⅰ.X .C 中的任何一个.自身连用构成数目.或者放在大数的右边连用构成数目.都不能超过三个:放在大数的左边只能用一个: 不能把基本数字 V .L .D 中的任何一个作为小数放在大数的左边采用相减的方法构成数目:放在大数的右边采用相加的方式构成数目.只能使用一个: V 和 X 左边的小数字只能用 Ⅰ: L 和 C 左边的小数字只能用X: D

Leetcode#12Integer to Roman

Integer to Roman Total Accepted: 31922 Total Submissions: 91429My Submissions Question Solution Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. Show Tags Have you met this question in a real

LeetCode——Integer to Roman

Description: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. noting to say. public class Solution { public String intToRoman(int number) { int[] values = {1000, 900, 500, 400, 100, 90, 50, 4

leetcode笔记:Roman to Integer

一.题目描述 Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 二.题目分析 还是先总结一下罗马数字,这是网上找到的一些解释: 罗马数字是最古老的数字表示方式,比阿拉伯数组早2000多年,起源于罗马- 罗马数字有如下符号: 基本字符: I V X L C D M 对应阿拉伯数字 :1 5 10 50 100 500 1000 计数规