Skip to content

Commit

Permalink
Tasks 28 & 66
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-pavlenko committed Mar 2, 2024
1 parent 35298d7 commit 78eb117
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 0 deletions.
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
}
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)
}
})
}
}
19 changes: 19 additions & 0 deletions 0066_Plus_One/0066_Plus_One.go
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
}
46 changes: 46 additions & 0 deletions 0066_Plus_One/0066_Plus_One_test.go
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)
}
})
}
}

0 comments on commit 78eb117

Please sign in to comment.