diff --git a/0028_Find_the_Index_of_the_First_Occurrence_in_a_String/0028_Find_the_Index_of_the_First_Occurrence_in_a_String.go b/0028_Find_the_Index_of_the_First_Occurrence_in_a_String/0028_Find_the_Index_of_the_First_Occurrence_in_a_String.go new file mode 100644 index 0000000..32a7e03 --- /dev/null +++ b/0028_Find_the_Index_of_the_First_Occurrence_in_a_String/0028_Find_the_Index_of_the_First_Occurrence_in_a_String.go @@ -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 +} diff --git a/0028_Find_the_Index_of_the_First_Occurrence_in_a_String/0028_Find_the_Index_of_the_First_Occurrence_in_a_String_test.go b/0028_Find_the_Index_of_the_First_Occurrence_in_a_String/0028_Find_the_Index_of_the_First_Occurrence_in_a_String_test.go new file mode 100644 index 0000000..ecd5237 --- /dev/null +++ b/0028_Find_the_Index_of_the_First_Occurrence_in_a_String/0028_Find_the_Index_of_the_First_Occurrence_in_a_String_test.go @@ -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) + } + }) + } +} diff --git a/0066_Plus_One/0066_Plus_One.go b/0066_Plus_One/0066_Plus_One.go new file mode 100644 index 0000000..086fba4 --- /dev/null +++ b/0066_Plus_One/0066_Plus_One.go @@ -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 +} diff --git a/0066_Plus_One/0066_Plus_One_test.go b/0066_Plus_One/0066_Plus_One_test.go new file mode 100644 index 0000000..9849c85 --- /dev/null +++ b/0066_Plus_One/0066_Plus_One_test.go @@ -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) + } + }) + } +}