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

Commit bc2aabb

Browse files
go: 118
1 parent 1f1e483 commit bc2aabb

File tree

14 files changed

+106
-19
lines changed

14 files changed

+106
-19
lines changed

‎README.md‎

Lines changed: 10 additions & 10 deletions
Large diffs are not rendered by default.

‎codes/go/leetcodes/go.mod‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module github.com/guobinhit/myleetcodes
2+
3+
go 1.15
4+
5+
require (
6+
github.com/stretchr/testify v1.7.0
7+
github.com/guobinhit/sylph v1.3.3
8+
)

‎codes/go/leetcodes/go.sum‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2+
github.com/guobinhit/sylph v1.3.3 h1:EUflyQ3MvrufyrGRC5sSo5ND94eeeASHw22zBMpUyrM=
3+
github.com/guobinhit/sylph v1.3.3/go.mod h1:nvTaI9mV07YK//oYVLGG9MGqX7oy5xid/DDxp1eT3YE=
4+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
6+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
7+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
8+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package easy_collection
2+
3+
/**
4+
* 118. Pascal's Triangle
5+
* <p>
6+
* Given numRows, generate the first numRows of Pascal's triangle.
7+
* <p>
8+
* For example, given numRows = 5, return
9+
* <p>
10+
* [
11+
* [1],
12+
* [1,1],
13+
* [1,2,1],
14+
* [1,3,3,1],
15+
* [1,4,6,4,1]
16+
* ]
17+
*/
18+
19+
func generate(numRows int) [][]int {
20+
var triangle [][]int
21+
if numRows <= 0 {
22+
return triangle
23+
}
24+
for i := 0; i < numRows; i++ {
25+
var row []int
26+
for j := 0; j < i+1; j++ {
27+
if j == 0 || j == i {
28+
row = append(row, 1)
29+
} else {
30+
/**
31+
* calculate element value:
32+
*
33+
* K(i)(j) = K(i-1)(j-1) + K(i-1)(j)
34+
*
35+
* except for the first and last element
36+
*/
37+
row = append(row, triangle[i-1][j-1]+triangle[i-1][j])
38+
}
39+
}
40+
triangle = append(triangle, row)
41+
}
42+
return triangle
43+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package easy_collection
2+
3+
import (
4+
"github.com/guobinhit/sylph/common/utils"
5+
"reflect"
6+
"testing"
7+
)
8+
9+
func Test_generate(t *testing.T) {
10+
type args struct {
11+
numRows int
12+
}
13+
tests := []struct {
14+
name string
15+
args args
16+
want string
17+
}{
18+
{
19+
name: "generate",
20+
args: args{numRows: 5},
21+
want: `[[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]`,
22+
},
23+
}
24+
for _, tt := range tests {
25+
t.Run(tt.name, func(t *testing.T) {
26+
got := generate(tt.args.numRows)
27+
if !reflect.DeepEqual(utils.Json(got), tt.want) {
28+
t.Errorf("generate() = %v, want %v", utils.Json(got), tt.want)
29+
}
30+
})
31+
}
32+
}

‎codes/go/leetcodes/interview/easy_collection/_283.go‎ renamed to ‎codes/go/leetcodes/interview/easy_collection/283.go‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,17 @@ package easy_collection
1818
* Minimize the total number of operations.
1919
*/
2020

21-
func moveZeroes(nums []int) {
21+
func moveZeroes(nums []int) []int{
2222
if len(nums) == 0 {
2323
return nums
2424
}
25-
26-
// move non zero number
2725
nonZeroLength := 0
2826
for _, n := range nums {
2927
if n != 0 {
3028
nums[nonZeroLength] = n
3129
nonZeroLength++
3230
}
3331
}
34-
35-
// fill zero number
3632
for ; nonZeroLength < len(nums); nonZeroLength++ {
3733
nums[nonZeroLength] = 0
3834
}

0 commit comments

Comments
(0)

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