We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent b27739d commit 98f9fe7Copy full SHA for 98f9fe7
LongestCommonPrefix.java
@@ -0,0 +1,23 @@
1
+/*
2
+ Author: King, [email protected]
3
+ Date: Dec 20, 2014
4
+ Problem: Longest Common Prefix
5
+ Difficulty: Easy
6
+ Source: https://oj.leetcode.com/problems/longest-common-prefix/
7
+ Notes:
8
+ Write a function to find the longest common prefix string amongst an array of strings.
9
+
10
+ Solution: ...
11
+ */
12
+public class Solution {
13
+ public String longestCommonPrefix(String[] strs) {
14
+ if (strs.length == 0) return new String("");
15
+ for(int i = 0;i < strs[0].length(); ++i){
16
+ for(int j = 1;j < strs.length; ++j){
17
+ if(i >= strs[j].length() || strs[j].charAt(i) != strs[0].charAt(i))
18
+ return strs[0].substring(0,i);
19
+ }
20
21
+ return strs[0];
22
23
+}
0 commit comments