-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
35298d7
commit 78eb117
Showing
4 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
...e_First_Occurrence_in_a_String/0028_Find_the_Index_of_the_First_Occurrence_in_a_String.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package leetcode_0028_Find_the_Index_of_the_First_Occurrence_in_a_String | ||
|
||
func strStr(haystack string, needle string) int { | ||
if len(needle) >= len(haystack) && needle != haystack { | ||
return -1 | ||
} | ||
|
||
haystack_len := len(haystack) | ||
needle_len := len(needle) | ||
res := -1 | ||
i := 0 | ||
j := 0 | ||
for i < haystack_len { | ||
if haystack[i] == needle[j] { | ||
if res == -1 { | ||
res = i | ||
} | ||
i++ | ||
j++ | ||
if j == needle_len { | ||
return res | ||
} | ||
} else if res >= 0 { | ||
i = res + 1 | ||
j = 0 | ||
res = -1 | ||
} else { | ||
i++ | ||
j = 0 | ||
} | ||
} | ||
if j < needle_len { | ||
return -1 | ||
} | ||
return res | ||
} |
63 changes: 63 additions & 0 deletions
63
...st_Occurrence_in_a_String/0028_Find_the_Index_of_the_First_Occurrence_in_a_String_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package leetcode_0028_Find_the_Index_of_the_First_Occurrence_in_a_String | ||
|
||
import "testing" | ||
|
||
func Test_strStr(t *testing.T) { | ||
type args struct { | ||
haystack string | ||
needle string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want int | ||
}{ | ||
{ | ||
name: "Test 1", | ||
args: args{ | ||
haystack: "xsadbutsad", | ||
needle: "sad", | ||
}, | ||
want: 1, | ||
}, | ||
{ | ||
name: "Test 2", | ||
args: args{ | ||
haystack: "leetcode", | ||
needle: "leeto", | ||
}, | ||
want: -1, | ||
}, | ||
{ | ||
name: "Test 3", | ||
args: args{ | ||
haystack: "sadbutsad", | ||
needle: "sad", | ||
}, | ||
want: 0, | ||
}, | ||
{ | ||
name: "Test 4", | ||
args: args{ | ||
haystack: "mississippi", | ||
needle: "issip", | ||
}, | ||
want: 4, | ||
}, | ||
{ | ||
name: "Test 5", | ||
args: args{ | ||
haystack: "mississi", | ||
needle: "issip", | ||
}, | ||
want: -1, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := strStr(tt.args.haystack, tt.args.needle); got != tt.want { | ||
t.Errorf("strStr() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package leetcode_0066_Plus_One | ||
|
||
func plusOne(digits []int) []int { | ||
inc := 1 | ||
for i := len(digits) - 1; i >= 0; i-- { | ||
digits[i] = digits[i] + inc | ||
if digits[i] == 10 { | ||
inc = 1 | ||
digits[i] = 0 | ||
} else { | ||
inc = 0 | ||
} | ||
} | ||
if inc > 0 { | ||
digits = append([]int{inc}, digits...) | ||
} | ||
|
||
return digits | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package leetcode_0066_Plus_One | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func Test_plusOne(t *testing.T) { | ||
type args struct { | ||
digits []int | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want []int | ||
}{ | ||
{ | ||
name: "Test 1", | ||
args: args{ | ||
digits: []int{1, 2, 3}, | ||
}, | ||
want: []int{1, 2, 4}, | ||
}, | ||
{ | ||
name: "Test 2", | ||
args: args{ | ||
digits: []int{4, 3, 2, 1}, | ||
}, | ||
want: []int{4, 3, 2, 2}, | ||
}, | ||
{ | ||
name: "Test 3", | ||
args: args{ | ||
digits: []int{9, 9, 9}, | ||
}, | ||
want: []int{1, 0, 0, 0}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := plusOne(tt.args.digits); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("plusOne() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |