【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.



题解:基本的罗马字符和数字对应如下表所示:

罗马字符 数字
I 1
V 5
X 10
L 50
C 100
D 500
M 1000

每隔一个字符又可以与相邻的两个字符组成一个新的数,例如I可以和V和X组成IV和IX,这样又可以生成6个数字,如下表:

罗马字符 数字
IV 4
IX 9
XL 40
XC 90
CD 400
CM 900

接下来我们用numbers记录上述13个数字,symbol数组记录上述13个符号,然后从最大的数1000开始,一步步将num转换成数字。

数字3012的转换过程如下:

  1. 3012/1000 = 3,answer = MMM,num <- 3012-3000=12;
  2. 12/900=0; 12/500 = 0; ......
  3. 12/10 = 1, answer = MMM+X=MMMX, num <- 12 - 10 = 2;
  4. 2/9 = 0,; 2/5 = 0; 2/4 = 0;
  5. 2/1 = 2, answer = MMMX+II = MMMXII, num <- 2-2 = 0;

所以3012对应的罗马数字为MMMXII。

代码如下:

 1 public class Solution {
 2     public String intToRoman(int num) {
 3         if(num <= 0)
 4             return "";
 5
 6         int[] numbers = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
 7         String[] symbol = {"M","CM","D","CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
 8
 9         StringBuffer answer = new StringBuffer();
10         int digit = 0;
11         while(num > 0){
12             int times = num / numbers[digit];
13             for(int i = 0;i < times;i++)
14                 answer.append(symbol[digit]);
15             num = num - numbers[digit] * times;
16             digit++;
17         }
18         return answer.toString();
19     }
20 }

【leetcode刷题笔记】Integer to Roman

时间: 2024-10-27 09:47:32

【leetcode刷题笔记】Integer to Roman的相关文章

【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. 题解:转换的方法:从左往右扫描罗马字符,如果当前的字符对应的数字比上一个数字小,就直接加上这个数字:否则加上这个数字并且减去上一个数字的两倍,然后更新上一个数字.利用一个HashMap存放罗马字符和数字的对应. 罗马数字和阿拉伯数字的对应表格参见http://www.cnblogs.

【leetcode刷题笔记】String to Integer (atoi)

Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes: It is intended for this problem to be spe

【leetcode刷题笔记】Longest Consecutive Sequence

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorithm should run in

【leetcode刷题笔记】Restore IP Addresses

Given a string containing only digits, restore it by returning all possible valid IP address combinations. For example:Given "25525511135", return ["255.255.11.135", "255.255.111.35"]. (Order does not matter) 题解:深度优先搜索.用resul

【leetcode刷题笔记】Max Points on a Line

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. 题解: 思路比较简单,每条直线都可以表示为y=kx+b,所以对于任意三点,如果它们共线,那么它们中任意两点的斜率都相等. 所以就遍历points数组,对其中的每一个元素计算它和位于它后面的数组元素的斜率并保存在一个hashmap中. 这个hashmap的键就是两点构成直线的斜率,值就是和当前元素po

【leetcode刷题笔记】Spiral Matrix II

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example,Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 题解:以前做过的Spiral Matrix是给一个矩阵螺旋式的输出,这道题是给一个n,螺旋式的

【leetcode刷题笔记】Substring with Concatenation of All Words

You are given a string, S, and a list of words, L, that are all of the same length. Find all starting indices of substring(s) in S that is a concatenation of each word in L exactly once and without any intervening characters. For example, given:S: "b

【leetcode刷题笔记】Validate Binary Search Tree

Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys

【leetcode刷题笔记】4Sum

Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Note: Elements in a quadruplet (a,b,c,d) must be in non-descending order.