add new solutions
This commit is contained in:
parent
489fa73880
commit
0f5f9e331c
11 changed files with 539 additions and 0 deletions
45
solutions/4/q401/solution.go
Normal file
45
solutions/4/q401/solution.go
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
package q401
|
||||
|
||||
import "fmt"
|
||||
|
||||
func bin2time(leds []uint8) (string, bool) {
|
||||
hour := leds[0]<<3 + leds[1]<<2 + leds[2]<<1 + leds[3]
|
||||
minute := leds[4]<<5 +
|
||||
leds[5]<<4 +
|
||||
leds[6]<<3 +
|
||||
leds[7]<<2 +
|
||||
leds[8]<<1 +
|
||||
leds[9]
|
||||
if hour >= 12 || minute >= 60 {
|
||||
return "", false
|
||||
}
|
||||
return fmt.Sprintf("%d:%02d", hour, minute), true
|
||||
}
|
||||
|
||||
func iterate(remaining, offset int, leds []uint8, ret *[]string) {
|
||||
if 10-offset < remaining {
|
||||
return
|
||||
}
|
||||
if remaining == 0 {
|
||||
if r, ok := bin2time(leds); ok {
|
||||
*ret = append(*ret, r)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
leds[offset] = 1
|
||||
iterate(remaining-1, offset+1, leds, ret)
|
||||
leds[offset] = 0
|
||||
iterate(remaining, offset+1, leds, ret)
|
||||
}
|
||||
|
||||
func readBinaryWatch(turnedOn int) []string {
|
||||
if turnedOn >= 9 {
|
||||
return nil
|
||||
}
|
||||
ret := []string{}
|
||||
iterate(turnedOn, 0, make([]uint8, 10), &ret)
|
||||
return ret
|
||||
}
|
||||
|
||||
var _ = readBinaryWatch
|
||||
45
solutions/4/q445/solution.go
Normal file
45
solutions/4/q445/solution.go
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
package q445
|
||||
|
||||
type ListNode struct {
|
||||
Val int
|
||||
Next *ListNode
|
||||
}
|
||||
|
||||
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
|
||||
var a1, a2 []*ListNode
|
||||
for l1 != nil {
|
||||
a1 = append(a1, l1)
|
||||
l1 = l1.Next
|
||||
}
|
||||
for l2 != nil {
|
||||
a2 = append(a2, l2)
|
||||
l2 = l2.Next
|
||||
}
|
||||
|
||||
ret := make([]ListNode, max(len(a1), len(a2))+1)
|
||||
for i := range ret {
|
||||
if i < len(a1) {
|
||||
ret[i].Val += a1[len(a1)-1-i].Val
|
||||
}
|
||||
if i < len(a2) {
|
||||
ret[i].Val += a2[len(a2)-1-i].Val
|
||||
}
|
||||
if ret[i].Val > 9 {
|
||||
ret[i+1].Val += ret[i].Val / 10
|
||||
ret[i].Val %= 10
|
||||
}
|
||||
|
||||
if i > 0 {
|
||||
ret[i].Next = &ret[i-1]
|
||||
}
|
||||
}
|
||||
|
||||
for i := len(ret) - 1; i >= 0; i-- {
|
||||
if ret[i].Val > 0 {
|
||||
return &ret[i]
|
||||
}
|
||||
}
|
||||
return &ret[0]
|
||||
}
|
||||
|
||||
var _ = addTwoNumbers
|
||||
Loading…
Add table
Add a link
Reference in a new issue