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 ...
随机推荐
- awk使用笔记
awk特殊字符打印方法: 1.awk打印双引号: awk '{print "\""}' 2.awk打印单引号: awk '{print "'\''&quo ...
- C#-类型转换和引用转换
对象的引用可以被: 隐式地向上转换 显示的向下转换 向上转换 向上转换是指一个从一个基类指向一个子类: House house = new House(); Asset asset = house; ...
- mysql 基本知识 以及优化
msql的索引应用 1.hash索引 等于值比较时 2.tree索引 范围比较 like '%%'
- 复杂分布式架构下的计算治理之路:计算中间件 Linkis
前言 在当前的复杂分布式架构环境下,服务治理已经大行其道.但目光往下一层,从上层 APP.Service,到底层计算引擎这一层面,却还是各个引擎各自为政,Client-Server 模式紧耦合满天飞的 ...
- 【MySQL参数】-innodb_additional_mem_pool_size
原博客:https://yq.aliyun.com/articles/32384
- MyBatis从入门到精通(第6章):6.3 使用枚举或其他对象
6.3 使用枚举或其他对象 在 sys_role 表中存在一个字段 enabled,这个字段只有两个可选值,0 为禁用,1 为启用.但是在 SysRole 类中,我们使用的是 Integer enab ...
- win10开发环境下安装mongodb
MongoDB 下载 https://www.mongodb.com/download-center/community 在win10系统安装mongodb需要vc++运行库,如果没有则会提示“无法启 ...
- Linux-守护进程的引入
1.进程查看命令ps (1).ps -ajx 偏向显示各种有关的ID号 (2).ps -aux 偏向显示进程各种占用资源 2.向进程发送信号指令kill (1).kill -信号编号 进程ID,向一 ...
- 886C. Petya and Catacombs#墓室探险(set集合)
题目出处:http://codeforces.com/problemset/problem/886/C 题目大意:很多墓穴之间有通道,探险家来回穿梭并记录日志 日志规则:第一次到该墓穴计时间t,0&l ...
- 关于shopee平台接口(php)对接示例
2018年8月之后,shopee开始使用新接口,需要进行授权操作 1.授权 public function getAuth(){ /** * @param ShopApiShopee $model * ...