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

28. Implement strStr() #33

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 implement-str-str
Jan 2, 2019
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 @@ -40,6 +40,7 @@ continually updating 😃.
* [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`*
* [28. Implement strStr()](src/0028_implement_strstr/implement_strstr.go)   *`double index`*
* [67. Add Binary](./src/0067_add_binary/add_binary.go)   *`brute force`*
* [76. Minimum Window Substring](./src/0076_minimum_window_substring/minimum_window_substring.go)    *`sliding window`*
* [125. Valid Palindrome](./src/0125_valid_palindrome/valid_palindrome.go)   *`string;`*  *`double index`*
Expand Down
35 changes: 35 additions & 0 deletions src/0028_implement_strstr/implement_strstr.go
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
28. Implement strStr()
https://leetcode.com/problems/implement-strstr/

Implement strStr().

Return the index of the first occurrence of needle in haystack,
or -1 if needle is not part of haystack.

clarification:
For the purpose of this problem, we will return 0 when needle is an empty string.
*/
// time: 2019年01月02日

package implstr

// time complexity: O( (m-n+1)*n ), where m is len(haystack), n is len(needle)
// space complexity: O(1)
func strStr(haystack string, needle string) int {
if 0 == len(needle) {
return 0
}

for i, j := 0, 0; i <= len(haystack)-len(needle); i++ {
for j = 0; j < len(needle); j++ {
if haystack[i+j] != needle[j] {
break
}
}
if len(needle) == j {
return i
}
}
return -1
}
23 changes: 23 additions & 0 deletions src/0028_implement_strstr/implement_strstr_test.go
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package implstr

import "testing"

func TestStrStr(t *testing.T) {
type arg struct {
haystack string
needle string
}

testCases := []arg{
{haystack: "hello", needle: "ll"},
{haystack: "aaaaa", needle: "bba"},
{haystack: "hello"},
}

expected := []int{2, -1, 0}
for index, data := range testCases {
if res := strStr(data.haystack, data.needle); res != expected[index] {
t.Errorf("expected %d, got %d", 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 @@ -19,6 +19,7 @@
|0025|[Reverse Nodes in k-Group](./0025_reverse_nodes_in_k_group/reverse_node_k_group.go)|Hard|*`linked list`*|
|0026|[Remove Duplicates from Sorted Array](0026_remove_duplicates_from_sorted_array/rdfsa.go)|Easy|*`array;`* *`double index`*|
|0027|[Remove Element](0027_remove_element/remove_element.go)|Easy|*`array`*|
|0028|[28. Implement strStr()](0028_implement_strstr/implement_strstr.go)|Easy|*`double index`*|
|0033|[Search in Rotated Sorted Array](0033_search_in_rotated_sorted_array/search_in_rotated_sorted_array.go)|Medium|*`binary search`*|
|0034|[ Find First and Last Position of Element in Sorted Array](0034_find_first_and_last_position_of_element_in_sorted_array/find_first_and_last_position_of_element_in_sorted_array.go)|Medium|*`binary search`*|
|0061|[Rotate List](./0061_rotate_list/rotate_list.go)|Medium|*`linked list`*|
Expand Down

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