LeetCode Day 7
罗马数字包含以下七种字符: I, V, X, L,C,D 和 M。
字符 | I | V | X | L | C | D | M |
---|---|---|---|---|---|---|---|
数值 | 1 | 5 | 10 | 50 | 100 | 500 | 1000 |
例如, 罗马数字 2 写做 II ,即为两个并列的 1。12 写做 XII ,即为 X + II 。 27 写做 XXVII, 即为 XX + V + II 。
通常情况下,罗马数字中小的数字在大的数字的右边。但也存在特例,例如 4 不写做 IIII,而是 IV。数字 1 在数字 5 的左边,所表示的数等于大数 5 减小数 1 得到的数值 4 。同样地,数字 9 表示为 IX。这个特殊的规则只适用于以下六种情况:
I 可以放在 V (5) 和 X (10) 的左边,来表示 4 和 9。
X 可以放在 L (50) 和 C (100) 的左边,来表示 40 和 90。
C 可以放在 D (500) 和 M (1000) 的左边,来表示 400 和 900。
给定一个整数,将其转为罗马数字。输入确保在 1 到 3999 的范围内。
/**
* @param {number} num
* @return {string}
*/
var intToRoman = function (num) {
let roman = [['I', 'V', 'IX'], ['X', 'L', 'XC'], ['C', 'D', 'CM'], ['M']];
let place = 1;//1个位,2十位,3百位,4千位
let s = '';
while (num > 0) {
let r = num % 10;
num = (num - r) / 10;
switch (r) {
case 1:
case 2:
case 3: {
s = roman[place - 1][0].repeat(r) + s;
break;
}
case 4: {
s = roman[place - 1][0] + roman[place - 1][1] + s;
break;
}
case 5: {
s = roman[place - 1][1] + s;
break;
}
case 6:
case 7:
case 8: {
s = roman[place - 1][1] + roman[place - 1][0].repeat(r - 5) + s;
break;
}
case 9: {
s = roman[place - 1][2] + s;
break;
}
}
place++;
}
return s;
};
时间差相差无几的贪心算法实现:
/**
* @param {number} num
* @return {string}
*/
var intToRoman = function (num) {
let res = '';
let moneys = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
let moneyToStr = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"];
let index = 0;
while (num > 0) {
if (num >= moneys[index]) {
res += moneyToStr[index];
num -= moneys[index];
index--;
}
index++;
}
return res;
};
紧随上面的设定,给定一个罗马数字,将其转换成整数。输入确保在 1 到 3999 的范围内。
示例 1:
输入: "III"
输出: 3
思路:
- 这个很简单,判断当前的字符,如果比下一位小,就是负数;
- 如果大就是正数。
- 最后一位直接加上即可。
/**
* @param {string} s
* @return {number}
*/
var romanToInt = function (s) {
let level = ['I', 'V', 'X', 'L', 'C', 'D', 'M'];
let value = [1, 5, 10, 50, 100, 500, 1000];
let result = 0;
let lens = s.length;
for (let i = 0; i < lens - 1; i++) {
let cur = level.indexOf(s[i]);
if (cur < level.indexOf(s[i + 1])) {
result -= value[cur];
} else {
result += value[cur];
}
}
result += value[level.indexOf(s[lens - 1])];
return result;
};
LeetCode Day 7的更多相关文章
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
随机推荐
- css3 flex布局详解
原文链接: http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html?utm_source=tuicool https://www.cnblog ...
- POJ 1655 Balancing Act【树的重心模板题】
传送门:http://poj.org/problem?id=1655 题意:有T组数据,求出每组数据所构成的树的重心,输出这个树的重心的编号,并且输出重心删除后得到的最大子树的节点个数,如果个数相同, ...
- sublime3激活方法
激活方法参考这里 $ tail -n4 /etc/hosts # https://blog.csdn.net/DeMeng33/article/details/80536926 127.0.0.1 w ...
- matlab画图中的坐标轴设置
ax = gca; ax是个结构体,查看ax变量,可以看到所有可设置的属性.几个常见属性如下: 设置坐标轴字体大小,粗细,字体名 2014b之后版本: ax = gca; ax.FontSize = ...
- 面向对象 / MVC
MVC模式 : 是一种分层思想(软件设计典范) M-model 模型层 :主要负责业务代码和数据 V-view 视图层 : 主要负责展现展示 C-controller 控制层:负责分发请求返回数据 ...
- Anaconda环境安装
Anaconda环境安装 一.Anaconda Anaconda是Python的一个开源的发行版本,里面包含了很多科学计算相关的包,它和Python的关系就像linux系统中centos和Ubuntu ...
- 论文翻译——Dynamic Pooling and Unfolding Recursive Autoencoders for Paraphrase Detection
Dynamic Pooling and Unfolding Recursive Autoencoders for Paraphrase Detection 动态池和展开递归自动编码器的意译检测 论文地 ...
- doc文件转txt
doc文件转txt # -*- coding:utf-8 -*- # 安装pywin32包 http://sourceforge.net/projects/pywin32/files/pywin32/ ...
- C# 类的解构
C#对类的解构,必须在该类内实现Deconstruct方法,并且返回类型为void ,并用out参数返回各个部分. using System; using System.Text; namespace ...
- Linux centos 常用的命令
Linux centos 下载命令:wget Linux centos 访问命令:vi Linux centos 插入命令:i Linux centos 保存退出: :wq Linux centos ...