add new solutions
This commit is contained in:
parent
f960020cb4
commit
14cd11f1c7
4 changed files with 155 additions and 0 deletions
37
solutions/1/q199/solution.go
Normal file
37
solutions/1/q199/solution.go
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
package q199
|
||||
|
||||
type TreeNode struct {
|
||||
Val int
|
||||
Left *TreeNode
|
||||
Right *TreeNode
|
||||
}
|
||||
|
||||
func rightSideView(root *TreeNode) []int {
|
||||
ret := []int{}
|
||||
|
||||
type qElem struct {
|
||||
height int
|
||||
node *TreeNode
|
||||
}
|
||||
|
||||
queue := []qElem{{0, root}}
|
||||
var last *qElem
|
||||
for ; len(queue) > 0; queue = queue[1:] {
|
||||
cur := &queue[0]
|
||||
if cur.node == nil {
|
||||
continue
|
||||
}
|
||||
if last != nil && last.height < cur.height {
|
||||
ret = append(ret, last.node.Val)
|
||||
}
|
||||
last = cur
|
||||
queue = append(queue, qElem{cur.height + 1, cur.node.Left}, qElem{cur.height + 1, cur.node.Right})
|
||||
}
|
||||
if last != nil {
|
||||
ret = append(ret, last.node.Val)
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
var _ = rightSideView
|
||||
31
solutions/2/q200/solution.go
Normal file
31
solutions/2/q200/solution.go
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
package q200
|
||||
|
||||
const (
|
||||
VISITED byte = '.'
|
||||
LAND byte = '1'
|
||||
)
|
||||
|
||||
func traverse(grid [][]byte, x, y int) int {
|
||||
if x < 0 || y < 0 || x >= len(grid[0]) || y >= len(grid) || grid[y][x] != LAND {
|
||||
return 0
|
||||
}
|
||||
|
||||
grid[y][x] = VISITED
|
||||
traverse(grid, x+1, y)
|
||||
traverse(grid, x-1, y)
|
||||
traverse(grid, x, y+1)
|
||||
traverse(grid, x, y-1)
|
||||
return 1
|
||||
}
|
||||
|
||||
func numIslands(grid [][]byte) int {
|
||||
count := 0
|
||||
for y := range grid {
|
||||
for x := range grid[y] {
|
||||
count += traverse(grid, x, y)
|
||||
}
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
var _ = numIslands
|
||||
37
solutions/5/q530/solution.go
Normal file
37
solutions/5/q530/solution.go
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
package q530
|
||||
|
||||
import "math"
|
||||
|
||||
type TreeNode struct {
|
||||
Val int
|
||||
Left *TreeNode
|
||||
Right *TreeNode
|
||||
}
|
||||
|
||||
func getMinimumDifference(root *TreeNode) int {
|
||||
minDiff := math.MaxInt
|
||||
var last *int
|
||||
|
||||
var check = func(num int) {
|
||||
if last == nil {
|
||||
last = &num
|
||||
return
|
||||
}
|
||||
minDiff = min(minDiff, num-*last)
|
||||
*last = num
|
||||
}
|
||||
var traverse func(node *TreeNode)
|
||||
traverse = func(node *TreeNode) {
|
||||
if node == nil {
|
||||
return
|
||||
}
|
||||
traverse(node.Left)
|
||||
check(node.Val)
|
||||
traverse(node.Right)
|
||||
}
|
||||
traverse(root)
|
||||
|
||||
return minDiff
|
||||
}
|
||||
|
||||
var _ = getMinimumDifference
|
||||
50
solutions/9/q909/solution.go
Normal file
50
solutions/9/q909/solution.go
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
package q909
|
||||
|
||||
func coord(n, num int) (x, y int) {
|
||||
num -= 1
|
||||
row := num / n
|
||||
y = n - row - 1
|
||||
x = num % n
|
||||
if row%2 == 1 {
|
||||
x = n - 1 - x
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func snakesAndLadders(board [][]int) int {
|
||||
n, sqN := len(board), len(board)*len(board)
|
||||
leastRolls := make([]int, sqN)
|
||||
if len(leastRolls) == 1 {
|
||||
return 0
|
||||
}
|
||||
|
||||
queue := make([]int, 0, sqN)
|
||||
queue = append(queue, 1)
|
||||
for ; len(queue) > 0; queue = queue[1:] {
|
||||
cur := queue[0]
|
||||
for dist := 1; dist <= 6 && cur+dist <= sqN; dist++ {
|
||||
next := cur + dist
|
||||
x, y := coord(n, next)
|
||||
if board[y][x] != -1 {
|
||||
next = board[y][x]
|
||||
}
|
||||
rolls := leastRolls[cur-1] + 1
|
||||
|
||||
if (next > 1 && leastRolls[next-1] == 0) || rolls < leastRolls[next-1] {
|
||||
leastRolls[next-1] = rolls
|
||||
if next == sqN {
|
||||
return rolls
|
||||
}
|
||||
queue = append(queue, next)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
l := leastRolls[sqN-1]
|
||||
if l == 0 {
|
||||
return -1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
var _ = snakesAndLadders
|
||||
Loading…
Add table
Add a link
Reference in a new issue