leetcode之旅(11)-Integer to Roman
2016-03-29 23:05
246 查看
题目描述:
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. Subscribe to see which companies asked this question Show Tags Show Similar Problems
准备知识看上篇博客
leetcode之旅(10)-Roman to Integer思路:
当前这个数是否大于最大的权?若大于则循环判断有几个这样的权?若小于,则降低权进行判断,重复判断操作
代码:
public class Solution { public String intToRoman(int num) { 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 result = ""; for (int i = 0; i < 13; i++) { while (num >= values[i]) //循环判断当前权值个数 { num -= values[i]; result = result +numerals[i]; //append函数是向string 的后面追加字符或字符串。 } } return result; } }
相关文章推荐
- 书评:《算法之美( Algorithms to Live By )》
- 动易2006序列号破解算法公布
- Ruby实现的矩阵连乘算法
- C#插入法排序算法实例分析
- 超大数据量存储常用数据库分表分库算法总结
- C#数据结构与算法揭秘二
- C#冒泡法排序算法实例分析
- 算法练习之从String.indexOf的模拟实现开始
- C#算法之关于大牛生小牛的问题
- C#实现的算24点游戏算法实例分析
- c语言实现的带通配符匹配算法
- 浅析STL中的常用算法
- 算法之排列算法与组合算法详解
- C++实现一维向量旋转算法
- Ruby实现的合并排序算法
- C#折半插入排序算法实现方法
- 基于C++实现的各种内部排序算法汇总
- C++线性时间的排序算法分析
- C++实现汉诺塔算法经典实例
- PHP实现克鲁斯卡尔算法实例解析