LeetCode0012

罗马数字包含以下七种字符: 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;
};

LeetCode0013

紧随上面的设定,给定一个罗马数字,将其转换成整数。输入确保在 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的更多相关文章

  1. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  2. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  3. [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 ...

  4. 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 ...

  5. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  6. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  7. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  8. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  9. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  10. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

随机推荐

  1. python中ndarray和matrix

    1. 定义ndarray和matrix from numpy import * a = mat([[1,2],[3,4]]) b = mat([[5,6],[7,8]]) c = array([1,2 ...

  2. SeetaFaceEngine系列2:Face Alignment编译和使用

    前面一篇写了编译人脸检测部分,现在就介绍下人脸配准部分,SeetaFace的Face Alignment通过人脸的五个关键点来配准人脸,也就是双眼.鼻尖.两个嘴角. 这部分的编译也和上一篇一样,步骤如 ...

  3. mysql增删查改

    <?php     $db_host   = 'localhost:3306';     $db_user   = 'root';     $db_passwd = '';     $db_na ...

  4. python 网络请求、下载

    #获取qq群中的群成员qq号import requestsmembers = []url = 'https://qun.qq.com/cgi-bin/qun_mgr/search_group_memb ...

  5. nvm安装教程

    nvm是一个nodejs的版本管理工具 默认安装位置  C:\Users\userName\AppData\Roaming\nvm x   1 C:\Users\userName\AppData\Ro ...

  6. [原]排错实战——解决Tekla通过.tsep安装插件失败的问题

    原总结调试排错troubleshootteklaprocess monitorsysinternals 缘起 最近同事使用.tsep安装Tekla插件的时候,Tekla提示该插件已经存在了,需要卸载后 ...

  7. Tensorflow学习教程------读取数据、建立网络、训练模型,小巧而完整的代码示例

    紧接上篇Tensorflow学习教程------tfrecords数据格式生成与读取,本篇将数据读取.建立网络以及模型训练整理成一个小样例,完整代码如下. #coding:utf-8 import t ...

  8. 帝国CMS7.5后台美化模板 后台风格修改 帝国CMS后台模板

    都知道帝国CMS功能强悍,生成静态html也非常好用.可是有时候他的后台样式,丑的让你不想用,dede呢,漏洞太多,PHPCMS好看,可是门槛要求高,你会写PHP才行. 帝国CMS后台美化模板:全面美 ...

  9. 884A. Book Reading#抽空学习好孩子(模拟)

    题目出处:http://codeforces.com/problemset/problem/884/A 题目大意:每天时间分两部分,工作和学习,工作优先,闲暇读书,问第几天读完 #include< ...

  10. 支付宝H5支付demo

    支付宝H5支付 首先我们必须注册一个支付宝应用(本案例就直接用支付宝的沙箱环境,这个沙箱也就是支付宝提供给开发者的一个测试环境) 登录地址:https://open.alipay.com/platfo ...