Skip to content

Commit bad4d98

Browse files
committed
commit
1 parent 8a664b5 commit bad4d98

File tree

37 files changed

+1136
-335
lines changed

37 files changed

+1136
-335
lines changed

.idea/modules.xml

Lines changed: 6 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

[005][Longest Palindromic Substring Total]/src/Solution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class Solution {
2323
* 要求出基准情况才能套用以上的公式:
2424
*
2525
* a. i + 1 = j -1,即回文长度为1时,dp[ i ][ i ] = true;
26-
* b. i +1 = (j - 1) -1,即回文长度为2时,dp[ i ][ i + 1] = s[ i ] == s[ i + 1]
26+
* b. i +1 = (j - 1) -1,即回文长度为2时,dp[ i ][ i + 1] = (s[ i ] == s[ i + 1])
2727
*
2828
* 有了以上分析就可以写出代码了。需要注意的是动态规划需要额外的O(n^2)的空间。
2929
* </pre>

[012][Integer To Roman]/src/Main.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import org.junit.Assert;
2+
import org.junit.Test;
3+
4+
/**
5+
* @author: wangjunchao(王俊超)
6+
* @time: 2019-06-17 14:03
7+
**/
8+
public class Main {
9+
10+
@Test
11+
public void test1() {
12+
Solution2 solution = new Solution2();
13+
Assert.assertEquals("III", solution.intToRoman(3));
14+
}
15+
16+
@Test
17+
public void test2() {
18+
Solution2 solution = new Solution2();
19+
Assert.assertEquals("IV", solution.intToRoman(4));
20+
}
21+
22+
@Test
23+
public void test3() {
24+
Solution2 solution = new Solution2();
25+
Assert.assertEquals("IX", solution.intToRoman(9));
26+
}
27+
28+
@Test
29+
public void test4() {
30+
Solution2 solution = new Solution2();
31+
Assert.assertEquals("LVIII", solution.intToRoman(58));
32+
}
33+
34+
@Test
35+
public void test5() {
36+
Solution2 solution = new Solution2();
37+
Assert.assertEquals("MCMXCIV", solution.intToRoman(1994));
38+
}
39+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* Author: 王俊超
3+
* Date: 2015-06-30
4+
* Time: 14:28
5+
* Declaration: All Rights Reserved !!!
6+
*/
7+
public class Solution2 {
8+
/**
9+
* <pre>
10+
* Given an integer, convert it to a roman numeral.
11+
*
12+
* Input is guaranteed to be within the range from 1 to 3999.
13+
*
14+
* 罗马数字的表示:
15+
* 个位数举例
16+
* (I, 1) (II, 2) (III, 3) (IV, 4) (V, 5) (VI, 6) (VII, 7) (VIII, 8) (IX, 9)
17+
*
18+
* 十位数举例
19+
* (X, 10) (XI, 11) (XII, 12) (XIII, 13) (XIV, 14) (XV, 15) (XVI, 16)
20+
* (XVII, 17) (XVIII, 18) (XIX, 19) (XX, 20) (XXI, 21) (XXII, 22)
21+
* (XXIX, 29) (XXX, 30) (XXXIV, 34) (XXXV, 35) (XXXIX, 39) (XL, 40)
22+
* (L, 50) (LI, 51) (LV, 55) (LX, 60) (LXV, 65) (LXXX, 80) (XC, 90)
23+
* (XCIII, 93) (XCV, 95) (XCVIII, 98) (XCIX, 99)
24+
*
25+
* 百位数举例
26+
* (C, 100) (CC, 200) (CCC, 300) (CD, 400) (D, 500) (DC, 600) (DCC, 700)
27+
* (DCCC, 800) (CM, 900) (CMXCIX, 999)
28+
*
29+
* 千位数举例
30+
* (M, 1000) (MC, 1100) (MCD, 1400) (MD, 1500) (MDC, 1600) (MDCLXVI, 1666)
31+
* (MDCCCLXXXVIII, 1888) (MDCCCXCIX, 1899) (MCM, 1900) (MCMLXXVI, 1976)
32+
* (MCMLXXXIV, 1984) (MCMXC, 1990) (MM, 2000) (MMMCMXCIX, 3999)
33+
*
34+
* 题目大意:
35+
* 输入一个数字,将它转成一个罗马数字,输入的数字在[1, 3999]之间
36+
*
37+
* </pre>
38+
*
39+
* @param num
40+
* @return
41+
*/
42+
public String intToRoman(int num) {
43+
44+
final int[] radix = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
45+
final String[] symbol = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
46+
String roman = "";
47+
for (int i = 0; num > 0; ++i) {
48+
int count = num / radix[i];
49+
num %= radix[i];
50+
for (; count > 0; --count) {
51+
roman += symbol[i];
52+
}
53+
}
54+
return roman;
55+
}
56+
}

[013][Romar To Integer]/src/Main.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import org.junit.Assert;
2+
import org.junit.Test;
3+
14
/**
25
* Author: 王俊超
36
* Date: 2015-03-01
@@ -25,4 +28,10 @@ public static void main(String[] args) {
2528
System.out.println(solution.romanToInt("MCMLXXVI"));
2629
System.out.println(solution.romanToInt("MMMCMXCIX"));
2730
}
31+
32+
@Test
33+
public void test1() {
34+
Solution solution = new Solution();
35+
Assert.assertEquals(1994, solution.romanToInt("MCMXCIV"));
36+
}
2837
}

[013][Romar To Integer]/src/Solution.java

Lines changed: 22 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ public class Solution {
1616
* 输入的数字在1-3999之间。
1717
*
1818
* 解题思路
19-
* 根据罗马数字与整数数字对应关系进行加法操作,如果前一个数字比后一个大就相减,否则进行相加。
19+
* 从前往后扫描,用一个临时变量记录分段数字。
20+
* 如果当前比前一个大,说明这一段的值应该是当前这个值减去上一个值。比如IV = 5 – 1;否
21+
* 则,将当前值加入到结果中,然后开始下一段记录。比如VI = 5 + 1, II=1+1
2022
* </pre>
2123
*
2224
* @param s
@@ -25,74 +27,29 @@ public class Solution {
2527
public int romanToInt(String s) {
2628

2729
int result = 0;
28-
int prev = 0; // 记录前一个数字的值
2930

30-
for (int i = s.length() - 1; i > -1; i--) {
31-
switch (s.charAt(i)) {
32-
case 'I': // 1
33-
if (1 < prev) {
34-
result -= 1;
35-
} else {
36-
result += 1;
37-
38-
}
39-
prev = 1;
40-
break;
41-
42-
case 'V': // 5
43-
44-
if (5 < prev) {
45-
result -= 5;
46-
} else {
47-
result += 5;
48-
}
49-
50-
prev = 5;
51-
52-
break;
53-
case 'X': // 10
54-
if (10 < prev) {
55-
result -= 10;
56-
} else {
57-
result += 10;
58-
}
59-
60-
prev = 10;
61-
break;
62-
case 'L': // 50
63-
if (50 < prev) {
64-
result -= 50;
65-
} else {
66-
result += 50;
67-
}
68-
69-
prev = 50;
70-
break;
71-
case 'C': // 100
72-
if (100 < prev) {
73-
result -= 100;
74-
} else {
75-
result += 100;
76-
}
77-
78-
prev = 100;
79-
break;
80-
case 'D': // 500
81-
if (500 < prev) {
82-
result -= 500;
83-
} else {
84-
result += 500;
85-
}
86-
87-
prev = 500;
88-
break;
89-
case 'M': // 1000
90-
result += 1000;
91-
prev = 1000;
92-
break;
31+
for (int i = 0; i < s.length(); i++) {
32+
// 当前的值比前一个值大,说明[i-1, i]组成一个值,并且值是s[i-1]-s[i]
33+
if (i > 0 && charToInt(s.charAt(i)) > charToInt(s.charAt(i - 1))) {
34+
// 要减去两倍之前前值才能回到真实值
35+
result += charToInt(s.charAt(i)) - 2 * charToInt(s.charAt(i - 1));
36+
} else {
37+
result += charToInt(s.charAt(i));
9338
}
9439
}
9540

9641
return result;
9742
}
43+
private int charToInt(char c) {
44+
switch (c) {
45+
case 'I': return 1;
46+
case 'V': return 5;
47+
case 'X': return 10;
48+
case 'L': return 50;
49+
case 'C': return 100;
50+
case 'D': return 500;
51+
case 'M': return 1000;
52+
default: return 0;
53+
}
54+
}
9855
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/**
2+
* Author: 王俊超
3+
* Date: 2015-03-01
4+
* Time: 16:09
5+
* Declaration: All Rights Reserved !!!
6+
*/
7+
public class Solution2 {
8+
/**
9+
* <pre>
10+
* 原题
11+
* Given a roman numeral, convert it to an integer.
12+
* Input is guaranteed to be within the range from 1 to 3999.
13+
*
14+
* 题目大意
15+
* 给定一个罗马数字,将其转换成对应的整数。
16+
* 输入的数字在1-3999之间。
17+
*
18+
* 解题思路
19+
* 根据罗马数字与整数数字对应关系进行加法操作,如果前一个数字比后一个大就相减,否则进行相加。
20+
* </pre>
21+
*
22+
* @param s
23+
* @return
24+
*/
25+
public int romanToInt(String s) {
26+
27+
int result = 0;
28+
int prev = 0; // 记录前一个数字的值
29+
30+
for (int i = s.length() - 1; i > -1; i--) {
31+
switch (s.charAt(i)) {
32+
case 'I': // 1
33+
if (1 < prev) {
34+
result -= 1;
35+
} else {
36+
result += 1;
37+
38+
}
39+
prev = 1;
40+
break;
41+
42+
case 'V': // 5
43+
44+
if (5 < prev) {
45+
result -= 5;
46+
} else {
47+
result += 5;
48+
}
49+
50+
prev = 5;
51+
52+
break;
53+
case 'X': // 10
54+
if (10 < prev) {
55+
result -= 10;
56+
} else {
57+
result += 10;
58+
}
59+
60+
prev = 10;
61+
break;
62+
case 'L': // 50
63+
if (50 < prev) {
64+
result -= 50;
65+
} else {
66+
result += 50;
67+
}
68+
69+
prev = 50;
70+
break;
71+
case 'C': // 100
72+
if (100 < prev) {
73+
result -= 100;
74+
} else {
75+
result += 100;
76+
}
77+
78+
prev = 100;
79+
break;
80+
case 'D': // 500
81+
if (500 < prev) {
82+
result -= 500;
83+
} else {
84+
result += 500;
85+
}
86+
87+
prev = 500;
88+
break;
89+
case 'M': // 1000
90+
result += 1000;
91+
prev = 1000;
92+
break;
93+
}
94+
}
95+
96+
return result;
97+
}
98+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import org.junit.Assert;
2+
import org.junit.Test;
3+
4+
/**
5+
* @author: wangjunchao(王俊超)
6+
* @time: 2019-06-17 13:05
7+
**/
8+
public class Main {
9+
@Test
10+
public void test1() {
11+
Solution solution = new Solution();
12+
Assert.assertEquals("fl", solution.longestCommonPrefix(new String[]{"flower", "flow", "flight"}));
13+
}
14+
15+
@Test
16+
public void test2() {
17+
Solution solution = new Solution();
18+
Assert.assertEquals("", solution.longestCommonPrefix(new String[]{"dog","racecar","car"}));
19+
}
20+
}

0 commit comments

Comments
 (0)