Skip to content

Commit e971838

Browse files
committed
Added task 14.
1 parent 7a40b13 commit e971838

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package s0014.longest.common.prefix;
2+
3+
public class Solution {
4+
public String longestCommonPrefix(String[] strs) {
5+
if (strs.length < 1) return "";
6+
if (strs.length == 1) return strs[0];
7+
8+
String temp = strs[0];
9+
10+
int i = 1;
11+
String cur;
12+
13+
while (temp.length() > 0 && i < strs.length) {
14+
if (temp.length() > strs[i].length()) {
15+
temp = temp.substring(0, strs[i].length());
16+
}
17+
18+
cur = strs[i].substring(0, temp.length());
19+
if (!cur.equals(temp)) {
20+
temp = temp.substring(0, temp.length() - 1);
21+
} else {
22+
i++;
23+
}
24+
}
25+
26+
return temp;
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package s0014.longest.common.prefix;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.Test;
7+
8+
public class SolutionTest {
9+
@Test
10+
public void romanToInt() {
11+
assertThat(
12+
new Solution().longestCommonPrefix(new String[] {"flower", "flow", "flight"}),
13+
equalTo("fl"));
14+
}
15+
}

0 commit comments

Comments
 (0)