(表盘上的四点钟“IIII”例外) 在一个数的上面画一条横线,表示这个数扩大1000倍。 1publicclassSolution {2publicString intToRoman(intnum) {3String [] I = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};4String [] X = {"", "X", "XX", "XXX", "XL"...
SOLUTION 1: 从大到小的贪心写法。从大到小匹配,尽量多地匹配当前的字符。 罗马数字对照表: http://literacy.kent.edu/Minigrants/Cinci/romanchart.htm View Code 2015.1.12 redo: View Code 请至主页君GITHUB: https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/string/IntToRoman.java...
https://leetcode.com/problems/integer-to-roman/submissions/ 用Java写: package com.leetcode.integerToRoman; public class Solution { public String intToRoman(int num) { int[] a = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; String[] b = {"M", "CM", "D", ...
publicclassSolution{ publicStringintToRoman(intnum) {String[] symbol = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};int[] value = {1000,900,500,400,100,90,50,40,10,9,5,4,1}; StringBuilder res =newStringBuilder();inti =0;while(num>0){if(num>...
题目: 原题链接: https://leetcode-cn.com/problems/integer-to-roman/ 解题思路: 贪心算法 建立映射表,数字从大到小的计算每个字符的个数 代码实现: class Solution: def intToRoman(self, num: int) -> str: val_map = {1000 : 'M', 900 : 'CM', 500 ...
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. Example 1: Input: 3 Output: "III" Example 2: Input: 4 Output: "IV" Example 3: Input: 9 Output: "IX" Example 4: ...
题目描述 请将给出的罗马数字转化为整数 保证输入的数字范围在1 到 3999之间。 分析 都加起来,找出反作用的,减二倍。 java代码 publicclassSolution{publicintromanToInt(Strings){if(s==null||s.length()==0){return0;}intres=0;if(s.indexOf("CM")!=-1)res-=200;if(s.indexOf("CD")!=-1)res...
The time and space complexity of the above approach isO(n), where n is the length of the given roman numeral string. Let's see another solution for the same. RomanToInteger2.java importjava.io.*; importjava.util.*; publicclassRomanToInteger2 ...
Java publicclassSolution{publicintromanToInt(Strings){if(s==null||s.length()==0)return0;Map<Character,Integer>m=newHashMap<Character,Integer>();m.put('I',1);m.put('V',5);m.put('X',10);m.put('L',50);m.put('C',100);m.put('D',500);m.put('M',1000);intlength=s.len...
https://leetcode.com/problems/integer-to-roman/discuss/6310/My-java-solution-easy-to-understand publicStringintToRoman(intnum){int[]values={1000,900,500,400,100,90,50,40,10,9,5,4,1};String[]strs={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};Stri...