LeetCode: Integer to Roman 解题报告

Integer to Roman

Given an integer, convert it to a roman numeral.

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

SOLUTION 1:

从大到小的贪心写法。从大到小匹配,尽量多地匹配当前的字符。

罗马数字对照表:

http://literacy.kent.edu/Minigrants/Cinci/romanchart.htm

 1 package Algorithms.string;
 2
 3 public class IntToRoman {
 4     public String intToRoman(int num) {
 5         int nums[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
 6         String[] romans = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
 7
 8         StringBuilder sb = new StringBuilder();
 9
10         int i = 0;
11         // 使用贪心法。尽量拆分数字
12         while (i < nums.length) {
13             if (num >= nums[i]) {
14                 sb.append(romans[i]);
15                 num -= nums[i];
16             } else {
17                 i++;
18             }
19         }
20
21         return sb.toString();
22     }
23 }

请至主页君GITHUB:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/string/IntToRoman.java

时间: 2024-10-11 00:05:46

LeetCode: Integer to Roman 解题报告的相关文章

Integer to Roman ——解题报告

[题目] Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 分析: 首先,我们需要知道罗马数字的表示方法,可参考链接:http://blog.csdn.net/ljiabin/article/details/39968583 然后,根据罗马数字的表示方法,我们可以把输入的num分成每一位,对应位转换成罗马数字即可. 具体实现的时候,

LeetCode: Pascal&#39;s Triangle 解题报告

Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] SOLUTION 1:很easy的题.注意记得把List加到ret中.比较简单,每一行的每一个元素有这个规律:1. 左右2边的是1.i, j 表示行,列坐标.2.

[LeetCode]Longest Valid Parentheses, 解题报告

题目 Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example i

【LeetCode】Insert Interval 解题报告

[题目] Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Example 1: Given intervals [1,3],[6,9], insert and m

【LeetCode】Subsets II 解题报告

[题目] Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,2], a solutio

LeetCode: Unique Paths II 解题报告

Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty spac

【LeetCode】3Sum Closest 解题报告

[题目] Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S = {

【LeetCode】Symmetric Tree 解题报告

Symmetric Tree 解题报告 [LeetCode] https://leetcode.com/problems/symmetric-tree/ Total Accepted: 106639 Total Submissions: 313969 Difficulty: Easy Question Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For

【LeetCode】Next Permutation 解题报告

[题目] Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order). The repl