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 ...
随机推荐
- 用Axure画原型图有感
感觉前端做UE非常有优势啊- 但是在制作的时候,似乎陷入了误区: (1)只求原型图的漂亮,色彩丰富,忽略了其本质作用,是用来整理逻辑,画出逻辑流程的. (2)一开始就追求交互,高保真的原型,忽视了细节 ...
- Kerbernetes的Service资源管理
Kerbernetes的Service资源管理 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Service概述 1>.为什么需要Service资源 我们知道在Kube ...
- mysql安装完之后,登陆后发现只有两个数据库
mysql安装完之后,登陆后发现只有两个数据库:mysql> show databases;+--------------------+| Database |+------ ...
- linux系统终端介绍
https://zhidao.baidu.com/question/174261014.html
- spring中的Filter使用
https://blog.csdn.net/bibiwannbe/article/details/81302920
- 吴裕雄--天生自然ShellX学习笔记:Shell echo命令
Shell 的 echo 指令与 PHP 的 echo 指令类似,都是用于字符串的输出.命令格式: echo string 您可以使用echo实现更复杂的输出格式控制. 1.显示普通字符串: echo ...
- LNMP应用
1.LNMP架构概述 LNMP就是Linux+Nginx+MySQL+PHP Linux作为服务器的操作系统 Nginx作为Web服务器 PHP作为解析动态脚本语言 MySQL即为数据库 Nginx服 ...
- webpack--删除dist目录
1.安装clean-webpack-plugin插件 npm install clean-webpack-plugin --D 2.在webpack.dev.conf.js或者webpack.conf ...
- MySql数据库,查询数据导出时会出现重复的记录(数据越多越明显)
在查询数据时,数据量多的时候,我们会使用分页功能. 每页显示多少数据. 这种情况下,一半看不出什么问题. 而导出数据时,有时就是通过分页的方法,逐步讲数据追加到导出文件中. 当全部数据都导出之后,就有 ...
- linuxmint截图
利用import命令截图,并设置快捷键 shift + PrtScrSysRq: 选中区域截图. 设置快捷键的时候,提示: Ctrl + PrtScrSysRq: 复制截图到剪切板 PrtScrSys ...