Skip to content

Commit 2e22d76

Browse files
author
chenweijie
committed
add test 9 13 14
1 parent b5899be commit 2e22d76

File tree

5 files changed

+273
-0
lines changed

5 files changed

+273
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.chen.algorithm.study.test13;
2+
3+
import org.junit.Test;
4+
5+
/**
6+
* @author : chen weijie
7+
* @Date: 2019-09-04 23:18
8+
*/
9+
public class Solution {
10+
11+
12+
public int romanToInt(String s) {
13+
14+
if (s == null || "".equals(s)) {
15+
return 0;
16+
}
17+
18+
char[] chars = s.toCharArray();
19+
Integer[] integers = new Integer[chars.length];
20+
21+
for (int i = 0; i < chars.length; i++) {
22+
23+
switch (chars[i]) {
24+
case 'I':
25+
integers[i] = 1;
26+
continue;
27+
case 'V':
28+
integers[i] = 5;
29+
continue;
30+
case 'X':
31+
integers[i] = 10;
32+
continue;
33+
case 'L':
34+
integers[i] = 50;
35+
continue;
36+
case 'C':
37+
integers[i] = 100;
38+
continue;
39+
case 'D':
40+
integers[i] = 500;
41+
continue;
42+
case 'M':
43+
integers[i] = 1000;
44+
continue;
45+
default:
46+
}
47+
}
48+
49+
50+
int firstValue = 0;
51+
int nextValue = 0;
52+
int sum = 0;
53+
54+
55+
for (int i = 0; i < integers.length; i++) {
56+
firstValue = integers[i];
57+
58+
if (i == s.length() - 1) {
59+
sum += firstValue;
60+
} else {
61+
nextValue = integers[i + 1];
62+
if (firstValue >= nextValue) {
63+
sum += firstValue;
64+
} else {
65+
sum -= firstValue;
66+
}
67+
}
68+
69+
70+
}
71+
return sum;
72+
}
73+
74+
75+
@Test
76+
public void testCase() {
77+
System.out.println(romanToInt("IV"));
78+
}
79+
80+
81+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.chen.algorithm.study.test14;
2+
3+
import org.junit.Test;
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
/**
9+
*
10+
* 错误
11+
*
12+
*
13+
* @author : chen weijie
14+
* @Date: 2019-09-05 00:17
15+
*/
16+
public class Solution {
17+
18+
19+
public String longestCommonPrefix(String[] strs) {
20+
21+
if (strs == null || strs.length == 0) {
22+
23+
return "";
24+
}
25+
26+
27+
String result;
28+
Map<String, String> map = new HashMap<>();
29+
30+
loop:
31+
for (int j = 0; ; j++) {
32+
for (int i = 0; i < strs.length; i++) {
33+
if (strs[i] == null || "".equals(strs[i])) {
34+
return "";
35+
}
36+
37+
if (i == 0) {
38+
map.put(strs[i].substring(0, j), strs[i].substring(0, j));
39+
continue;
40+
}
41+
if (!map.containsKey(strs[i].substring(0, j))) {
42+
result = strs[i].substring(0, j - 1);
43+
break loop;
44+
}
45+
}
46+
}
47+
return result;
48+
49+
}
50+
51+
@Test
52+
public void testCase(){
53+
54+
55+
String [] strings = {""};
56+
57+
System.out.println(longestCommonPrefix(strings));
58+
59+
60+
61+
}
62+
63+
64+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.chen.algorithm.study.test14;
2+
3+
import org.junit.Test;
4+
5+
/**
6+
* @author : chen weijie
7+
* @Date: 2019-09-05 00:55
8+
*/
9+
public class Solution2 {
10+
11+
12+
public String longestCommonPrefix(String[] strs) {
13+
14+
if (strs.length == 0) {
15+
return "";
16+
}
17+
18+
String prefix = strs[0];
19+
for (int i = 1; i < strs.length; i++) {
20+
while (!strs[i].startsWith(prefix)) {
21+
prefix = prefix.substring(0, prefix.length() - 1);
22+
if (prefix.isEmpty()) {
23+
return "";
24+
}
25+
}
26+
}
27+
return prefix;
28+
}
29+
30+
31+
@Test
32+
public void testCase(){
33+
34+
35+
String [] strings = {"flower","flow","flight"};
36+
37+
System.out.println(longestCommonPrefix(strings));
38+
39+
40+
41+
}
42+
43+
44+
45+
46+
47+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.chen.algorithm.study.test9;
2+
3+
import org.junit.Test;
4+
5+
/**
6+
* @author : chen weijie
7+
* @Date: 2019-09-04 22:54
8+
*/
9+
public class Solution {
10+
11+
12+
public boolean isPalindrome(int x) {
13+
14+
if (x < 0) {
15+
return false;
16+
}
17+
18+
char[] chars = String.valueOf(x).toCharArray();
19+
20+
StringBuilder sb = new StringBuilder();
21+
for (int i = chars.length - 1; i >= 0; i--) {
22+
sb.append(chars[i]);
23+
}
24+
25+
return x == Long.parseLong(sb.toString());
26+
}
27+
28+
29+
30+
@Test
31+
public void testCase(){
32+
System.out.println(isPalindrome(2147483647));
33+
}
34+
35+
36+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.chen.algorithm.study.test9;
2+
3+
import org.junit.Test;
4+
5+
/**
6+
* @author : chen weijie
7+
* @Date: 2019-09-04 23:09
8+
*/
9+
public class Solution2 {
10+
11+
12+
// 通过计算 1221 / 1000, 得首位1
13+
// 通过计算 1221 % 10, 可得末位 1
14+
15+
public boolean isPalindrome(int x) {
16+
//边界判断
17+
if (x < 0) {
18+
return false;
19+
}
20+
21+
int div = 1;
22+
//
23+
while (x / div >= 10) {
24+
div *= 10;
25+
}
26+
27+
while (x > 0) {
28+
int left = x / div;
29+
int right = x % 10;
30+
if (left != right) {
31+
return false;
32+
}
33+
x = (x % div) / 10;
34+
div /= 100;
35+
}
36+
return true;
37+
}
38+
39+
40+
@Test
41+
public void testCase() {
42+
System.out.println(isPalindrome(121));
43+
}
44+
45+
}

0 commit comments

Comments
 (0)