Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 64ed8f0

Browse filesBrowse files
committed
feat: add 012
1 parent 1b031f7 commit 64ed8f0
Copy full SHA for 64ed8f0

File tree

3 files changed

+71
-0
lines changed
Filter options

3 files changed

+71
-0
lines changed

‎README.md

Copy file name to clipboardExpand all lines: README.md
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
| 6 | [ZigZag Conversion][006] | String |
7474
| 8 | [String to Integer (atoi)][008] | Math, String |
7575
| 11 | [Container With Most Water][011] | Array, Two Pointers |
76+
| 12 | [Integer to Roman][012] | Math, String |
7677
| 15 | [3Sum][015] | Array, Two Pointers |
7778
| 17 | [Letter Combinations of a Phone Number][017] | String, Backtracking |
7879
| 19 | [Remove Nth Node From End of List][019] | Linked List, Two Pointers |
@@ -143,6 +144,7 @@
143144
[006]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/006/README.md
144145
[008]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/008/README.md
145146
[011]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/011/README.md
147+
[012]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/012/README.md
146148
[015]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/015/README.md
147149
[017]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/017/README.md
148150
[019]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/019/README.md

‎note/012/README.md

Copy file name to clipboard
+44Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# [Container With Most Water][title]
2+
3+
## Description
4+
5+
Given an integer, convert it to a roman numeral.
6+
7+
Input is guaranteed to be within the range from 1 to 3999.
8+
9+
**Tags:** Math, String
10+
11+
12+
## 思路
13+
14+
题意是整型数转罗马数字,范围从 1 到 3999,查看下百度百科的罗马数字介绍如下:
15+
16+
* 相同的数字连写,所表示的数等于这些数字相加得到的数,如 Ⅲ=3;
17+
18+
* 小的数字在大的数字的右边,所表示的数等于这些数字相加得到的数,如 Ⅷ=8、Ⅻ=12;
19+
20+
* 小的数字(限于 Ⅰ、X 和 C)在大的数字的左边,所表示的数等于大数减小数得到的数,如 Ⅳ=4、Ⅸ=9。
21+
22+
那么我们可以把整数的每一位分离出来,让其每一位都用相应的罗马数字位表示,最终拼接完成。比如 `621` 我们可以分离百、十、个分别为 `6``2``1`,那么 `600` 对应的罗马数字是 `DC``20` 对应的罗马数字是 `XX``1` 对应的罗马数字是 `I`,所以最终答案便是 `DCXXI`
23+
24+
```java
25+
class Solution {
26+
public String intToRoman(int num) {
27+
String M[] = {"", "M", "MM", "MMM"};
28+
String C[] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
29+
String X[] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
30+
String I[] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
31+
return M[num / 1000] + C[(num % 1000) / 100] + X[(num % 100) / 10] + I[num % 10];
32+
}
33+
}
34+
```
35+
36+
37+
## 结语
38+
39+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-java-leetcode][ajl]
40+
41+
42+
43+
[title]: https://leetcode.com/problems/container-with-most-water
44+
[ajl]: https://github.com/Blankj/awesome-java-leetcode
+25Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.blankj.medium._012;
2+
3+
/**
4+
* <pre>
5+
* author: Blankj
6+
* blog : http://blankj.com
7+
* time : 2018/01/25
8+
* desc :
9+
* </pre>
10+
*/
11+
public class Solution {
12+
public String intToRoman(int num) {
13+
String M[] = {"", "M", "MM", "MMM"};
14+
String C[] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
15+
String X[] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
16+
String I[] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
17+
return M[num / 1000] + C[(num % 1000) / 100] + X[(num % 100) / 10] + I[num % 10];
18+
}
19+
20+
public static void main(String[] args) {
21+
Solution solution = new Solution();
22+
System.out.println(solution.intToRoman(621));// DCXXI
23+
System.out.println(solution.intToRoman(348));// CCCXLVIII
24+
}
25+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.