mirror of https://github.com/01-edu/public.git
Xavier Petit
5 years ago
committed by
xpetit
55 changed files with 397 additions and 360 deletions
@ -1,23 +1,23 @@ |
|||||||
package solutions |
package solutions |
||||||
|
|
||||||
type TNode struct { |
type TNode struct { |
||||||
Val int |
Val int |
||||||
Left *TNode |
Left *TNode |
||||||
Right *TNode |
Right *TNode |
||||||
} |
} |
||||||
|
|
||||||
func Invert(root *TNode) { |
func Invert(root *TNode) { |
||||||
if root != nil { |
if root != nil { |
||||||
temp := root.Left |
temp := root.Left |
||||||
root.Left = root.Right |
root.Left = root.Right |
||||||
root.Right = temp |
root.Right = temp |
||||||
|
|
||||||
Invert(root.Left) |
Invert(root.Left) |
||||||
Invert(root.Right) |
Invert(root.Right) |
||||||
} |
} |
||||||
} |
} |
||||||
|
|
||||||
func InvertTree(root *TNode) *TNode { |
func InvertTree(root *TNode) *TNode { |
||||||
Invert(root) |
Invert(root) |
||||||
return root |
return root |
||||||
} |
} |
||||||
|
@ -1,19 +1,19 @@ |
|||||||
package solutions |
package solutions |
||||||
|
|
||||||
//This solution is the comparing file of the staff
|
// This solution is the comparing file of the staff
|
||||||
// Because the solution is a function,
|
// Because the solution is a function,
|
||||||
//
|
//
|
||||||
//1) here the package is solutions
|
// 1) here the package is solutions
|
||||||
//2) it does not need an empty func main(){}
|
// 2) it does not need an empty func main(){}
|
||||||
//3) its location is 1 level below the folder of the nauuo_test.go file
|
// 3) its location is 1 level below the folder of the nauuo_test.go file
|
||||||
|
|
||||||
func gcd(first, second int) int { |
func gcd(first, second int) int { |
||||||
if (second == 0) { |
if second == 0 { |
||||||
return (first) |
return first |
||||||
} |
} |
||||||
return (gcd(second, first % second)) |
return gcd(second, first%second) |
||||||
} |
} |
||||||
|
|
||||||
func Lcm(first, second int) int { |
func Lcm(first, second int) int { |
||||||
return (first / gcd(second, first % second) * second) |
return first / gcd(second, first%second) * second |
||||||
} |
} |
||||||
|
@ -1,27 +1,27 @@ |
|||||||
package solutions |
package solutions |
||||||
|
|
||||||
type TreeNodeL struct { |
type TreeNodeL struct { |
||||||
Left *TreeNodeL |
Left *TreeNodeL |
||||||
Val int |
Val int |
||||||
Right *TreeNodeL |
Right *TreeNodeL |
||||||
} |
} |
||||||
|
|
||||||
func IsSameTree(p *TreeNodeL, q *TreeNodeL) bool { |
func IsSameTree(p *TreeNodeL, q *TreeNodeL) bool { |
||||||
if (p == nil && q == nil) { |
if p == nil && q == nil { |
||||||
return true |
return true |
||||||
} |
} |
||||||
if (checkIfEq(p, q) == true) { |
if checkIfEq(p, q) == true { |
||||||
return true |
return true |
||||||
} |
} |
||||||
return false |
return false |
||||||
} |
} |
||||||
|
|
||||||
func checkIfEq(t1 *TreeNodeL, t2 *TreeNodeL) bool { |
func checkIfEq(t1 *TreeNodeL, t2 *TreeNodeL) bool { |
||||||
if (t1 == nil && t2 == nil) { |
if t1 == nil && t2 == nil { |
||||||
return true |
return true |
||||||
} |
} |
||||||
if (t1 == nil || t2 == nil) { |
if t1 == nil || t2 == nil { |
||||||
return false; |
return false |
||||||
} |
} |
||||||
return (t1.Val == t2.Val && checkIfEq(t1.Right, t2.Right) && checkIfEq(t1.Left, t2.Left)) |
return (t1.Val == t2.Val && checkIfEq(t1.Right, t2.Right) && checkIfEq(t1.Left, t2.Left)) |
||||||
} |
} |
||||||
|
@ -1,31 +1,31 @@ |
|||||||
package main |
package main |
||||||
|
|
||||||
type TreeNodeL struct { |
type TreeNodeL struct { |
||||||
Left *TreeNodeL |
Left *TreeNodeL |
||||||
Val int |
Val int |
||||||
Right *TreeNodeL |
Right *TreeNodeL |
||||||
} |
} |
||||||
|
|
||||||
func IsSameTree(p *TreeNodeL, q *TreeNodeL) bool { |
func IsSameTree(p *TreeNodeL, q *TreeNodeL) bool { |
||||||
if (p == nil && q == nil) { |
if p == nil && q == nil { |
||||||
return true |
return true |
||||||
} |
} |
||||||
if (checkIfEq(p, q) == true) { |
if checkIfEq(p, q) == true { |
||||||
return true |
return true |
||||||
} |
} |
||||||
return false |
return false |
||||||
} |
} |
||||||
|
|
||||||
func checkIfEq(t1 *TreeNodeL, t2 *TreeNodeL) bool { |
func checkIfEq(t1 *TreeNodeL, t2 *TreeNodeL) bool { |
||||||
if (t1 == nil && t2 == nil) { |
if t1 == nil && t2 == nil { |
||||||
return true |
return true |
||||||
} |
} |
||||||
if (t1 == nil || t2 == nil) { |
if t1 == nil || t2 == nil { |
||||||
return false; |
return false |
||||||
} |
} |
||||||
return (t1.Val == t2.Val && checkIfEq(t1.Right, t2.Right) && checkIfEq(t1.Left, t2.Left)) |
return (t1.Val == t2.Val && checkIfEq(t1.Right, t2.Right) && checkIfEq(t1.Left, t2.Left)) |
||||||
} |
} |
||||||
|
|
||||||
func main() { |
func main() { |
||||||
|
|
||||||
} |
} |
||||||
|
@ -1,17 +1,17 @@ |
|||||||
package main |
package main |
||||||
|
|
||||||
import ( |
import ( |
||||||
"math/rand" |
"math/rand" |
||||||
"testing" |
"testing" |
||||||
|
|
||||||
"github.com/01-edu/z01" |
|
||||||
solutions "../../solutions" |
solutions "../../solutions" |
||||||
|
"github.com/01-edu/z01" |
||||||
) |
) |
||||||
|
|
||||||
func TestTwoSum(t *testing.T) { |
func TestTwoSum(t *testing.T) { |
||||||
for i := 0; i < 20; i++ { |
for i := 0; i < 20; i++ { |
||||||
token := rand.Perm(20) |
token := rand.Perm(20) |
||||||
target := rand.Intn(30) |
target := rand.Intn(30) |
||||||
z01.Challenge(t, TwoSum, solutions.TwoSum, token, target) |
z01.Challenge(t, TwoSum, solutions.TwoSum, token, target) |
||||||
} |
} |
||||||
} |
} |
||||||
|
Loading…
Reference in new issue