Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 21262fc

Browse files
authored
14 solved. (#29)
1 parent 703e17b commit 21262fc

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ continually updating 😃.
3737

3838
### String
3939
* [3. Longest Substring Without Repeating Characters](./src/0003_longest_substring_without_repeating_characters/longest_substring_without_repeating_characters.go)   *`sliding window;`*  *`hash table`*
40+
* [14. Longest Common Prefix](src/0014_longest_common_prefix/lcp.go)
4041
* [17. Letter Combinations of a Phone Number](./src/0017_letter_combination_of_a_phone_number/letter_combination_of_phone_number.go)   *`tree`*
4142
* [20. Valid Parentheses](./src/0020_valid_parentheses/valid_parentheses.go)   *`stack`*
4243
* [67. Add Binary](./src/0067_add_binary/add_binary.go)   *`brute force`*

‎src/0014_longest_common_prefix/lcp.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
14. Longest Common Prefix
3+
https://leetcode.com/problems/longest-common-prefix/
4+
5+
Write a function to find the longest common prefix string amongst an array of strings.
6+
7+
If there is no common prefix, return an empty string "".
8+
*/
9+
// time: 2018年12月31日
10+
11+
package lcp
12+
13+
// time complexity: O(len(strs) * max len of string)
14+
// space complexity: O(1)
15+
func longestCommonPrefix(strs []string) string {
16+
if 0 == len(strs) {
17+
return ""
18+
}
19+
res := ""
20+
for i := 0; i < len(strs[0]); i++ {
21+
c := strs[0][i]
22+
for j := 1; j < len(strs); j++ {
23+
if i >= len(strs[j]) || strs[j][i] != c {
24+
return res
25+
}
26+
}
27+
res += string(c)
28+
}
29+
return res
30+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package lcp
2+
3+
import "testing"
4+
5+
func TestLongestCommonPrefix(t *testing.T) {
6+
testCases := [][]string{
7+
{"flower", "flow", "flight"},
8+
{"dog", "racecar", "car"},
9+
{},
10+
{""},
11+
}
12+
13+
expected := []string{
14+
"fl",
15+
"",
16+
"",
17+
"",
18+
}
19+
20+
for index, strs := range testCases {
21+
if res := longestCommonPrefix(strs); res != expected[index] {
22+
t.Errorf("expected %s, got %s", expected[index], res)
23+
}
24+
}
25+
}

‎src/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
|0009|[9. Palindrome Number](0009_palindrome_number/palindrome_number.go)|Easy|*`math`*|
1111
|0011|[11. Container With Most Water](0011_container_with_most_water/container_with_most_water.go)|Medium|*`array;`* *`double index`*|
1212
|0013|[13. Roman to Integer](0013_roman_to_integer/roman_to_integer.go)|Easy|*`math`*|
13+
|0014|[14. Longest Common Prefix](0014_longest_common_prefix/lcp.go)|Easy||
1314
|0017|[Letter Combinations of a Phone Number](0017_letter_combination_of_a_phone_number/letter_combination_of_phone_number.go)|Medium|*`tree`*|
1415
|0020|[Valid Parentheses](0020_valid_parentheses/valid_parentheses.go)|Easy|*`string;`* *`stack`*|
1516
|0021|[Merge Two Sorted Lists](0021_merge_two_sorted_lists/mergeTwoLists.go)|Easy|*`linked list`*|

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /