integer to roman leetcode c++实现
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
将整数用罗马数字表示。
思路分析:
{"","I","II","III","IV","V","VI","VII","VIII","IX"},
{"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"},
{"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"},
{"","M","MM","MMM"}
首先从输入的整数开始,(1)取其个位上的数值,判定后得到一个字符串(这个字符串也应该位于罗马数字的最右边,所以再用一个栈来解决)
(2)然后将整数除以10(轮到十位上的数值了),再去执行(1)
代码如下:
- #include<stack>
- class Solution {
- public:
- string intToRoman(int num) {
- vector<vector<string>> data={
- {"","I","II","III","IV","V","VI","VII","VIII","IX"},
- {"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"},
- {"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"},
- {"","M","MM","MMM"}
- };
- stack<string> sta;
- int i=;
- string res="",result="";
- while(num!=){
- int remain=num%;
- res=data[i][remain];
- sta.push(res);
- ++i;
- num/=;
- }
- while(!sta.empty()){
- result+=sta.top();
- sta.pop();
- }
- return result;
- }
- };
integer to roman leetcode c++实现的更多相关文章
- Integer to Roman - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Integer to Roman - LeetCode 注意点 考虑输入为0的情况 解法 解法一:从大到小考虑1000,900,500,400,100,9 ...
- Integer to Roman -- LeetCode 012
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- Integer To Roman leetcode java
问题描述: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range fr ...
- [LeetCode][Python]Integer to Roman
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/integer ...
- leetCode练题——12. Integer to Roman
1.题目 12. Integer to Roman Roman numerals are represented by seven different symbols: I, V, X, L, C, ...
- 【LeetCode】Roman to Integer & Integer to Roman
Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...
- 【leetcode】Integer to Roman
Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within t ...
- 【leetcode】Integer to Roman & Roman to Integer(easy)
Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...
- LeetCode:Roman to Integer,Integer to Roman
首先简单介绍一下罗马数字,一下摘自维基百科 罗马数字共有7个,即I(1).V(5).X(10).L(50).C(100).D(500)和M(1000).按照下述的规则可以表示任意正整数.需要注意的是罗 ...
随机推荐
- 【旧文章搬运】Idle进程相关的一些东西
原文发表于百度空间,2009-05-13========================================================================== Idle进 ...
- Unity脚本打包android工程
using UnityEngine; using System.Collections; using UnityEditor; public class NewBehaviourScript : Ed ...
- IT兄弟连 JavaWeb教程 重定向
HTTP协议规定了一种重定向机制,重定向的运作流程如下: ● 用户在浏览器端输入特定URL,请求访问服务器端的某个组件. ● 服务器端的组件返回一个状态码为302的响应结果,该响应结果的含义为: ...
- mac的日常使用总结
目录 有一个github的仓库:(强烈推荐) 不推荐的但是可以试试的一些链接: # 关于mac book的使用教程 github简直是一个宝藏,发现好多各种好玩的东西, 爱了爱了, 开源一定是未来, ...
- locale localedef --之Linux字符集理解
参考: https://www.cnblogs.com/dolphi/p/3622420.html http://www.360doc.com/content/15/1105/08/14513 ...
- 改变UITabbar顶部分割线颜色
项目中是使用UITabbarController 因此改变UITabbar的分割线代码如下 由于美术没提供图片,所以自己创建了个图片 //改变tabbar 线条颜色 CGRect rect = CGR ...
- iOS 上传的图片在HTML上显示时,图片方向信息(EXIF Orientation)异常
将iPhone 6s拍摄的照片上传到服务器之后, 在Web网页上看到图片被逆时针旋转了90度, 这让我很惆怅呐! 出现这个问题其实是因为上传的图片为.jpg格式,.jpg文件含有EXIF信息, 其中E ...
- 通过 xshell 连接 ubuntu on windows(WSL)
装上 ubuntu on windows 后,默认要先打开 cmd, 再运行 bash 进入 ubuntu 的 shell. 但是这个shell很难看,配色不好就算了,还存在各种复制粘贴麻烦. 默认没 ...
- Jamie and Binary Sequence (changed after round) - CodeForces 916B
http://codeforces.com/problemset/problem/916/B 好尬啊... #include<cstdio> #include<algorithm&g ...
- iOS中自定义UITableViewCell的用法
1.先创建一个View继承 UITableViewCell并使用xib快速建立模型. #import <UIKit/UIKit.h> #import "Score.h" ...