add new solutions
This commit is contained in:
parent
886b5e0a8e
commit
67cad91898
47 changed files with 1549 additions and 1 deletions
28
solutions/13/q1318/solution.go
Normal file
28
solutions/13/q1318/solution.go
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
package q1318
|
||||
|
||||
func minFlips(a int, b int, c int) int {
|
||||
aorb := a | b
|
||||
flips := 0
|
||||
|
||||
p := 1
|
||||
for p <= aorb || p <= c {
|
||||
switch {
|
||||
case c|p == c && aorb|p != aorb:
|
||||
flips++ // c has the bit
|
||||
|
||||
case c|p != c && aorb|p == aorb:
|
||||
if a|p == a {
|
||||
flips++ // a has the bit
|
||||
}
|
||||
if b|p == b {
|
||||
flips++ // b has the bit
|
||||
}
|
||||
}
|
||||
|
||||
p <<= 1
|
||||
}
|
||||
|
||||
return flips
|
||||
}
|
||||
|
||||
var _ = minFlips
|
||||
34
solutions/13/q1372/solution.go
Normal file
34
solutions/13/q1372/solution.go
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
package q1372
|
||||
|
||||
type TreeNode struct {
|
||||
Val int
|
||||
Left *TreeNode
|
||||
Right *TreeNode
|
||||
}
|
||||
|
||||
func zigZagLen(node *TreeNode, isLeft bool, curLen int) int {
|
||||
if node == nil {
|
||||
return 0
|
||||
}
|
||||
var l, r int
|
||||
if isLeft {
|
||||
l = zigZagLen(node.Left, true, 1)
|
||||
r = zigZagLen(node.Right, false, curLen+1)
|
||||
} else {
|
||||
l = zigZagLen(node.Left, true, curLen+1)
|
||||
r = zigZagLen(node.Right, false, 1)
|
||||
}
|
||||
return max(curLen, l, r)
|
||||
}
|
||||
|
||||
func longestZigZag(root *TreeNode) int {
|
||||
if root == nil {
|
||||
return 0
|
||||
}
|
||||
return max(
|
||||
zigZagLen(root.Left, true, 1),
|
||||
zigZagLen(root.Right, false, 1),
|
||||
)
|
||||
}
|
||||
|
||||
var _ = longestZigZag
|
||||
Loading…
Add table
Add a link
Reference in a new issue