Skip to content

Commit 08f16ad

Browse files
committed
add new files
1 parent b5ed682 commit 08f16ad

File tree

5 files changed

+104
-0
lines changed

5 files changed

+104
-0
lines changed

566. Reshape the Matrix.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
func matrixReshape(nums [][]int, r int, c int) [][]int {
4+
ret := make([][]int, r)
5+
for k := range ret {
6+
ret[k] = make([]int, c)
7+
}
8+
if len(nums) == 0 || r*c != len(nums)*len(nums[0]) {
9+
return nums
10+
}
11+
rows, cols := 0, 0
12+
for i := 0; i < len(nums); i++ {
13+
for j := 0; j < len(nums[0]); j++ {
14+
ret[rows][cols] = nums[i][j]
15+
cols++
16+
if cols == c {
17+
rows++
18+
cols = 0
19+
}
20+
}
21+
}
22+
return ret
23+
}

662. Maximum Width of Binary Tree.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package main
2+
3+
type TreeNode struct {
4+
Val int
5+
Left *TreeNode
6+
Right *TreeNode
7+
}
8+
9+
var ans int
10+
var left map[int]int
11+
12+
func widthOfBinaryTree(root *TreeNode) int {
13+
ans = 0
14+
left = make(map[int]int)
15+
dfs(root, 0, 0)
16+
return ans
17+
}
18+
func dfs(root *TreeNode, depth, pos int) {
19+
if root == nil {
20+
return
21+
}
22+
if _, ok := left[depth]; !ok {
23+
left[depth] = pos
24+
}
25+
ans = max(ans, pos-left[depth]+1)
26+
dfs(root.Left, depth+1, 2*pos)
27+
dfs(root.Right, depth+1, 2*pos+1)
28+
}
29+
30+
func max(x, y int) int {
31+
if x > y {
32+
return x
33+
}
34+
return y
35+
36+
}

766. Toeplitz Matrix.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package main
2+
3+
func isToeplitzMatrix(matrix [][]int) bool {
4+
for r := 0; r < len(matrix); r++ {
5+
for c := 0; c < len(matrix[0]); c++ {
6+
if r > 0 && c > 0 && matrix[r-1][c-1] != matrix[r][c] {
7+
return false
8+
}
9+
}
10+
}
11+
return true
12+
}

771. Jewels and Stones.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
func numJewelsInStones(J string, S string) int {
4+
jstr := []byte(J)
5+
sstr := []byte(S)
6+
m := make(map[byte]bool)
7+
for _, v := range jstr {
8+
m[v] = true
9+
}
10+
num := 0
11+
for _, v := range sstr {
12+
if _, ok := m[v]; ok {
13+
num++
14+
}
15+
}
16+
return num
17+
}

867. Transpose Matrix.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package main
2+
3+
func transpose(A [][]int) [][]int {
4+
m := len(A)
5+
n := len(A[0])
6+
ans := make([][]int, n)
7+
for k := range ans {
8+
ans[k] = make([]int, m)
9+
}
10+
for r := 0; r < m; r++ {
11+
for c := 0; c < n; c++ {
12+
ans[c][r] = A[r][c]
13+
}
14+
}
15+
return ans
16+
}

0 commit comments

Comments
 (0)