题目描述:

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. Xcode一种涉及到多桌面的调试技巧

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) Mac本身是支持多桌面功能的,以下是本猫OS界面的截图: 可以 ...

  2. C在控制台上实现鼠标画图功能

    #include <windows.h> #include <stdio.h> #include <string.h> HANDLE hOut; HANDLE hI ...

  3. 分布式缓存组件Hazelcast

    Hazelcast是一个Java的开源分布式内存实现,它具有以下特性: 提供java.util.{Queue, Set, List, Map}的分布式实现 提供java.util.concurrent ...

  4. Python图片处理库之PIL

    这个模块对于Python2.7 的windows64位电脑而言,还真的是不好找啊.这里分享一个下载链接吧,需要的朋友可以下载下来.PIL For Windows64 Python2.7下面分享一下这个 ...

  5. Android Studio设置代理更新下载SDK

    代理主机和端口号按下图设置即可,便可以轻松的下载更新SDK啦~~~

  6. linux下可执行文件的库们

    在Linux下有一些命令可以让我们知道可执行文件的很多信息. 记录如下: ldd : print shared library dependencies nm: list symbols from o ...

  7. Sqoop执行mysql删除语句

    如果使用Sqoop删除mysql中的数据,并且传递动态日期参数,则使用下方的方法: 创建一个sh文件,内容如下: #!/bin/sh ## 环境变量生效 . /etc/profile #[调度删除导入 ...

  8. [Err] 1093 - You can't specify target table 's' for update in FROM clause

    [Err] 1093 - You can't specify target table 's' for update in FROM clause 执行SQL DELETE from book WHE ...

  9. Android studio中的一次编译报错’Error:Execution failed for task ':app:transformClassesWithDexForDebug‘,困扰了两天

    先说下背景:随着各种第三方框架的使用,studio在编译打包成apk时,在dex如果发现有相同的jar包,不能创建dalvik虚拟机.一个apk,就是一个运行在linux上的一个虚拟机. 上图就是一直 ...

  10. 打包自己的aar库

    在比较大的 Android 项目的开发中,我们经常会遇到工程.jar 包等等之间相互引用的方式.一般我们通过在 gradle 文件中配置依赖来解决,但是如果通过include的方式来引入第三方库的时候 ...