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

14. Longest Common Prefix #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
zwfang merged 1 commit into master from longest-common-prefix
Dec 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ continually updating 😃.

### String
* [3. Longest Substring Without Repeating Characters](./src/0003_longest_substring_without_repeating_characters/longest_substring_without_repeating_characters.go)   *`sliding window;`*  *`hash table`*
* [14. Longest Common Prefix](src/0014_longest_common_prefix/lcp.go)
* [17. Letter Combinations of a Phone Number](./src/0017_letter_combination_of_a_phone_number/letter_combination_of_phone_number.go)   *`tree`*
* [20. Valid Parentheses](./src/0020_valid_parentheses/valid_parentheses.go)   *`stack`*
* [67. Add Binary](./src/0067_add_binary/add_binary.go)   *`brute force`*
Expand Down
30 changes: 30 additions & 0 deletions src/0014_longest_common_prefix/lcp.go
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
14. Longest Common Prefix
https://leetcode.com/problems/longest-common-prefix/

Write a function to find the longest common prefix string amongst an array of strings.

If there is no common prefix, return an empty string "".
*/
// time: 2018年12月31日

package lcp

// time complexity: O(len(strs) * max len of string)
// space complexity: O(1)
func longestCommonPrefix(strs []string) string {
if 0 == len(strs) {
return ""
}
res := ""
for i := 0; i < len(strs[0]); i++ {
c := strs[0][i]
for j := 1; j < len(strs); j++ {
if i >= len(strs[j]) || strs[j][i] != c {
return res
}
}
res += string(c)
}
return res
}
25 changes: 25 additions & 0 deletions src/0014_longest_common_prefix/lcp_test.go
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package lcp

import "testing"

func TestLongestCommonPrefix(t *testing.T) {
testCases := [][]string{
{"flower", "flow", "flight"},
{"dog", "racecar", "car"},
{},
{""},
}

expected := []string{
"fl",
"",
"",
"",
}

for index, strs := range testCases {
if res := longestCommonPrefix(strs); res != expected[index] {
t.Errorf("expected %s, got %s", expected[index], res)
}
}
}
1 change: 1 addition & 0 deletions src/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
|0009|[9. Palindrome Number](0009_palindrome_number/palindrome_number.go)|Easy|*`math`*|
|0011|[11. Container With Most Water](0011_container_with_most_water/container_with_most_water.go)|Medium|*`array;`* *`double index`*|
|0013|[13. Roman to Integer](0013_roman_to_integer/roman_to_integer.go)|Easy|*`math`*|
|0014|[14. Longest Common Prefix](0014_longest_common_prefix/lcp.go)|Easy||
|0017|[Letter Combinations of a Phone Number](0017_letter_combination_of_a_phone_number/letter_combination_of_phone_number.go)|Medium|*`tree`*|
|0020|[Valid Parentheses](0020_valid_parentheses/valid_parentheses.go)|Easy|*`string;`* *`stack`*|
|0021|[Merge Two Sorted Lists](0021_merge_two_sorted_lists/mergeTwoLists.go)|Easy|*`linked list`*|
Expand Down

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