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 ...
随机推荐
- Maven - No plugin found for prefix 'tomcat7' in the current project
问题发现: 在构建Maven项目的时候,出现了No plugin found for prefix 'tomcat7' in the current project的错误. 是需要在Maven的Pom ...
- Spring(一)——IOC和DI的简单理解
Spring是一个IOC(DI)和AOP容器框架,并且是开源的. 1.IOC和DI 比较官方的说法: •IOC(Inversion of Control):其思想是反转资源获取的方向. 传统的资源查找 ...
- linux设置网络三种方法
http://blog.csdn.net/u010003835/article/details/52233296
- 吴裕雄--天生自然 PHP开发学习:高级
<?php echo date("Y/m/d") . "<br>"; echo date("Y.m.d") . " ...
- Necklace HDU - 3874 (线段树/树状数组 + 离线处理)
Necklace HDU - 3874 Mery has a beautiful necklace. The necklace is made up of N magic balls. Each b ...
- centos快速安装mysql
1. wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm 2. rpm -ivh mysql-community-r ...
- cf1172E Nauuo and ODT(LCT)
首先可以转化问题,变为对每种颜色分别考虑不含该颜色的简单路径条数.然后把不是当前颜色的点视为白色,是当前颜色的点视为黑色,显然路径数量是每个白色连通块大小的平方和,然后题目变为:黑白两色的树,单点翻转 ...
- Python笔记_第二篇_面向过程_第二部分_6.包
1. 包: 思考:如果不同人的编写模块同名了怎么办? 解决:为了解决模块命名的冲突,引入了按目录来组织模块的方法,这个目录成为“包” 特点:引入了包以后,只要丁顶层的包,不与其他人发生冲突,那么模块都 ...
- 查看opencv-python编译信息
python -c "import cv2; print(cv2.getBuildInformation())" General configuration for OpenCV ...
- vue 利用axios请求接口下载excel
一般有三种方法: 方法一: 通过a标签下载 // href为文件的存储路径或者地址,download为问文件名 <a href="/images/download.jpg" ...