题目描述:

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;    

    }
}

leetcode之旅(11)-Integer to Roman的更多相关文章

  1. 【LeetCode】12 & 13 - Integer to Roman & Roman to Integer

    12 - Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be wit ...

  2. leetCode练题——12. Integer to Roman

    1.题目 12. Integer to Roman Roman numerals are represented by seven different symbols: I, V, X, L, C,  ...

  3. leetcode之旅(10)-Roman to Integer

    题目描述: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range fr ...

  4. leetcode解决问题的方法||Integer to Roman问题

    problem: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range ...

  5. leetcode第12题--Integer to Roman

    Problem: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range ...

  6. LeetCode(12)Integer to Roman

    题目 Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from ...

  7. [LeetCode][Python]Integer to Roman

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/integer ...

  8. [LeetCode] Integer to Roman 整数转化成罗马数字

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

  9. Integer to Roman - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Integer to Roman - LeetCode 注意点 考虑输入为0的情况 解法 解法一:从大到小考虑1000,900,500,400,100,9 ...

随机推荐

  1. Android简易实战教程--第二十二话《自定义组合控件模拟qq登录下拉框和其中的一些”小技巧”》

    转载此文章请注明出处:点击打开链接   http://blog.csdn.net/qq_32059827/article/details/52313516 首先,很荣幸此专栏能被CSDN推荐到主页.荣 ...

  2. 指令汇B新闻客户端开发(六) 浅谈屏幕适配解决方案

    屏幕适配的问题,我相信很多大牛的经验远比我丰富,在此就简单的分享一下我所做的的屏幕适配方案,当然我说的是安卓方面的啦,嘿嘿,屏幕适配我们一般用1280*720的屏幕作为我们的主流开发屏,当然现在And ...

  3. LATEX TEMPLATE (SPRINGER) (*.BST)

    该模板在哪里下载? http://www.springer.com/computer/image+processing/journal/11263, Instructions for Authors, ...

  4. 2.Lucene3.6.2包介绍,第一个Lucene案例介绍,查看索引信息的工具lukeall介绍,Luke查看的索引库内容,索引查找过程

     1  Lucen目录介绍 2  lucene-core-3.6.2.jar是lucene开发核心jar包 contrib  目录存放,包含一些扩展jar包 3  案例 建立第一个Lucene项目 ...

  5. UNIX环境高级编程——单实例的守护进程

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <signal.h&g ...

  6. 让 Google Test 出错时断点

    Google Test 缺省是出错退出. 如果最后的出错行在系统库中,那就没什么帮助. 如果是调试运行,直接退出根本就不知道哪里出错了. 后来添加了一个运行参数: --gtest_break_on_f ...

  7. request.setAttribute()怎么用的?

    request.setAttribute()怎么用的? JSP1代码 String [] test=new String[2]; test[0]="1"; test[1]=&quo ...

  8. UE4利用Save Game创建全局变量

    因为盲目的做了一个UE4的项目,没有用到UE4的无缝加载,我只能在一个个关卡中手动切换,然后每次的数据都会重置,这对于项目来说,造成了体验感的极度下降. 然而我查了一下怎样在UE4中创建全局变量,找到 ...

  9. (八十七)AutoLayout的简介与实例

    AutoLayout是继AutoResizing之后的一种自动布局方法,解决了AutoResizing无法处理控件间相互关系的问题. AutoLayout在storyboard中通过底部工具条设置,底 ...

  10. SendMessageUpwards定义简单按钮(Unity3D开发之十)

    猴子原创,欢迎转载.转载请注明: 转载自Cocos2D开发网–Cocos2Dev.com,谢谢! 原文地址: http://www.cocos2dev.com/?p=582 SendMessageUp ...