Skip to content

Commit 2a4a103

Browse files
author
Blankj
committed
add 013
1 parent 8551c1f commit 2a4a103

File tree

7 files changed

+94
-6
lines changed

7 files changed

+94
-6
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
|7|[Reverse Integer][007]|Math|
1111
|8|[String to Integer (atoi)][008]|Math, String|
1212
|9|[Palindrome Number][009]|Math|
13+
|13|[Roman to Integer][013]|Math, String|
1314

1415

1516
## Medium
@@ -34,3 +35,4 @@
3435
[007]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/007/README.md
3536
[008]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/008/README.md
3637
[009]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/009/README.md
38+
[013]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/013/README.md

note/001/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Because nums[0] + nums[1] = 2 + 7 = 9,
1515
return [0, 1].
1616
```
1717

18-
**Tags** Array, Hash Table
18+
**Tags:** Array, Hash Table
1919

2020

2121
## 思路0

note/007/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Reverse digits of an integer.
88

99
**Example2:** x = -123, return -321
1010

11-
**Spoilers**
11+
**Spoilers:**
1212

1313
**Have you thought about this?**
1414

@@ -24,7 +24,7 @@ For the purpose of this problem, assume that your function returns 0 when the re
2424

2525
The input is assumed to be a 32-bit signed integer. Your function should **return 0 when the reversed integer overflows**.
2626

27-
**Tags** Math
27+
**Tags:** Math
2828

2929

3030
## 思路

note/008/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Implement atoi to convert a string to an integer.
88

99
**Notes:** It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.
1010

11-
**Spoilers**
11+
**Spoilers:**
1212

1313
**Requirements for atoi:**
1414

note/009/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
Determine whether an integer is a palindrome. Do this without extra space.
66

7-
**spoilers:**
7+
**Spoilers:**
88

99
**Some hints:**
1010

@@ -16,7 +16,7 @@ You could also try reversing an integer. However, if you have solved the problem
1616

1717
There is a more generic way of solving this problem.
1818

19-
**Tags** Math
19+
**Tags:** Math
2020

2121

2222
## 思路0

note/013/README.md

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# [Roman to Integer](https://leetcode.com/problems/roman-to-integer/)
2+
3+
## Description
4+
5+
Given a roman numeral, convert it to an integer.
6+
7+
Input is guaranteed to be within the range from 1 to 3999.
8+
9+
**Tags:** Math, String
10+
11+
12+
## 思路0
13+
14+
题意是罗马数字转整型数,范围从1到3999,查看下百度百科的罗马数字介绍如下,
15+
16+
* 相同的数字连写,所表示的数等于这些数字相加得到的数,如 Ⅲ=3;
17+
* 小的数字在大的数字的右边,所表示的数等于这些数字相加得到的数,如 Ⅷ=8、Ⅻ=12;
18+
* 小的数字(限于 Ⅰ、X 和 C)在大的数字的左边,所表示的数等于大数减小数得到的数,如 Ⅳ=4、Ⅸ=9;
19+
20+
那么我们可以利用map来完成罗马数字的7个数字符号:I、V、X、L、C、D、M和整数的映射关系,然后根据上面的解释来模拟完成即可。
21+
22+
``` java
23+
public class Solution {
24+
public int romanToInt(String s) {
25+
Map<Character, Integer> map = new HashMap<>();
26+
map.put('I', 1);
27+
map.put('V', 5);
28+
map.put('X', 10);
29+
map.put('L', 50);
30+
map.put('C', 100);
31+
map.put('D', 500);
32+
map.put('M', 1000);
33+
int len = s.length();
34+
int sum = map.get(s.charAt(len - 1));
35+
for (int i = len - 2; i >= 0; --i) {
36+
if (map.get(s.charAt(i)) < map.get(s.charAt(i + 1))) {
37+
sum -= map.get(s.charAt(i));
38+
} else {
39+
sum += map.get(s.charAt(i));
40+
}
41+
}
42+
return sum;
43+
}
44+
}
45+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.blankj.easy._013;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* <pre>
8+
* author: Blankj
9+
* blog : http://blankj.com
10+
* time : 2017/04/22
11+
* desc :
12+
* </pre>
13+
*/
14+
public class Solution {
15+
public int romanToInt(String s) {
16+
Map<Character, Integer> map = new HashMap<>();
17+
map.put('I', 1);
18+
map.put('V', 5);
19+
map.put('X', 10);
20+
map.put('L', 50);
21+
map.put('C', 100);
22+
map.put('D', 500);
23+
map.put('M', 1000);
24+
int len = s.length();
25+
int sum = map.get(s.charAt(len - 1));
26+
for (int i = len - 2; i >= 0; --i) {
27+
if (map.get(s.charAt(i)) < map.get(s.charAt(i + 1))) {
28+
sum -= map.get(s.charAt(i));
29+
} else {
30+
sum += map.get(s.charAt(i));
31+
}
32+
}
33+
return sum;
34+
}
35+
36+
public static void main(String[] args) {
37+
Solution solution = new Solution();
38+
System.out.println(solution.romanToInt("DCXXI"));// 621
39+
System.out.println(solution.romanToInt("CCCXLVIII"));// 384
40+
}
41+
}

0 commit comments

Comments
 (0)