add new solutions
This commit is contained in:
parent
489fa73880
commit
0f5f9e331c
11 changed files with 539 additions and 0 deletions
5
.sqlfluff
Normal file
5
.sqlfluff
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
[sqlfluff]
|
||||
dialect = mysql
|
||||
exclude_rules = CP02, AM04
|
||||
|
||||
# vim: ft=toml
|
||||
49
solutions/0/q43/solution.go
Normal file
49
solutions/0/q43/solution.go
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
package q43
|
||||
|
||||
import "slices"
|
||||
|
||||
func multiply(num1, num2 string) string {
|
||||
if num1 == "0" || num2 == "0" {
|
||||
return "0"
|
||||
}
|
||||
|
||||
l1, l2 := len(num1), len(num2)
|
||||
res := make([]byte, l1+l2)
|
||||
|
||||
// Calculation
|
||||
for i1 := range l1 {
|
||||
n1 := num1[l1-1-i1] - '0'
|
||||
if n1 == 0 {
|
||||
continue
|
||||
}
|
||||
for i2 := range l2 {
|
||||
n2 := num2[l2-1-i2] - '0'
|
||||
j := i1 + i2
|
||||
res[j] += n1 * n2
|
||||
if res[j] > 9 {
|
||||
res[j+1] += res[j] / 10
|
||||
res[j] = res[j] % 10
|
||||
}
|
||||
}
|
||||
for i := l2 + i1; res[i] > 9; i++ {
|
||||
res[i+1] += res[i] / 10
|
||||
res[i] = res[i] % 10
|
||||
}
|
||||
}
|
||||
|
||||
// Post process
|
||||
for i := len(res) - 1; i >= 0; i-- {
|
||||
if res[i] > 0 {
|
||||
res = res[:i+1]
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
slices.Reverse(res)
|
||||
for i := range res {
|
||||
res[i] = res[i] + '0'
|
||||
}
|
||||
return string(res)
|
||||
}
|
||||
|
||||
var _ = multiply
|
||||
63
solutions/0/q68/solution.go
Normal file
63
solutions/0/q68/solution.go
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
package q68
|
||||
|
||||
var (
|
||||
emptyLine = make([]byte, 100)
|
||||
buf = make([]byte, 100)
|
||||
)
|
||||
|
||||
func init() {
|
||||
for i := range emptyLine {
|
||||
emptyLine[i] = ' '
|
||||
}
|
||||
}
|
||||
|
||||
func emitLine(line []byte, words []string, justifyLeft bool) string {
|
||||
spaces := len(line)
|
||||
for i := range words {
|
||||
spaces -= len(words[i])
|
||||
}
|
||||
lenSep := spaces / max(len(words)-1, 1)
|
||||
leftover := spaces % max(len(words)-1, 1)
|
||||
|
||||
offset := len(words[0])
|
||||
copy(line, words[0])
|
||||
for i := 1; i < len(words); i++ {
|
||||
var spaces = 1
|
||||
if !justifyLeft {
|
||||
spaces = lenSep
|
||||
if i <= leftover {
|
||||
spaces++
|
||||
}
|
||||
}
|
||||
offset += spaces
|
||||
copy(line[offset:], words[i])
|
||||
offset += len(words[i])
|
||||
}
|
||||
return string(line)
|
||||
}
|
||||
|
||||
func fullJustify(words []string, maxWidth int) []string {
|
||||
ret := []string{}
|
||||
line := buf[:maxWidth]
|
||||
copy(line, emptyLine)
|
||||
|
||||
i := 0
|
||||
lineLen := len(words[0])
|
||||
for j := 1; j < len(words); j++ {
|
||||
if lineLen+1+len(words[j]) <= maxWidth {
|
||||
lineLen += 1 + len(words[j])
|
||||
continue
|
||||
}
|
||||
|
||||
ret = append(ret, emitLine(line, words[i:j], false))
|
||||
copy(line, emptyLine)
|
||||
|
||||
i = j
|
||||
lineLen = len(words[j])
|
||||
}
|
||||
|
||||
// Last line
|
||||
return append(ret, emitLine(line, words[i:], true))
|
||||
}
|
||||
|
||||
var _ = fullJustify
|
||||
56
solutions/0/q8/solution.go
Normal file
56
solutions/0/q8/solution.go
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
package q8
|
||||
|
||||
const (
|
||||
MIN = -(1 << 31)
|
||||
MAX = 1<<31 - 1
|
||||
)
|
||||
|
||||
func myAtoi(s string) int {
|
||||
sign := 0
|
||||
num := 0
|
||||
|
||||
Loop:
|
||||
for i := range len(s) {
|
||||
c := s[i]
|
||||
|
||||
switch c {
|
||||
case '-':
|
||||
if sign != 0 {
|
||||
break Loop
|
||||
}
|
||||
sign = -1
|
||||
|
||||
case '+':
|
||||
if sign != 0 {
|
||||
break Loop
|
||||
}
|
||||
sign = 1
|
||||
|
||||
case ' ':
|
||||
if sign != 0 {
|
||||
break Loop
|
||||
}
|
||||
|
||||
default:
|
||||
if c < '0' || c > '9' {
|
||||
break Loop
|
||||
}
|
||||
if sign == 0 {
|
||||
sign = 1
|
||||
}
|
||||
num = num*10 + int(c-'0')
|
||||
if sign*num < MIN {
|
||||
num = -MIN
|
||||
break Loop
|
||||
}
|
||||
if sign*num > MAX {
|
||||
num = MAX
|
||||
break Loop
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return sign * num
|
||||
}
|
||||
|
||||
var _ = myAtoi
|
||||
40
solutions/10/q1041/solution.go
Normal file
40
solutions/10/q1041/solution.go
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
package q1041
|
||||
|
||||
type Dir int8
|
||||
|
||||
const (
|
||||
UP Dir = iota
|
||||
RIGHT
|
||||
DOWN
|
||||
LEFT
|
||||
)
|
||||
|
||||
func (d Dir) TurnL() Dir { return (d + 3) % 4 }
|
||||
func (d Dir) TurnR() Dir { return (d + 1) % 4 }
|
||||
|
||||
var vects = [][2]int{{0, -1}, {1, 0}, {0, 1}, {-1, 0}}
|
||||
|
||||
func isRobotBounded(instructions string) bool {
|
||||
x, y, d := 0, 0, UP
|
||||
|
||||
for range 4 {
|
||||
for i := range len(instructions) {
|
||||
switch instructions[i] {
|
||||
case 'G':
|
||||
x += vects[d][0]
|
||||
y += vects[d][1]
|
||||
case 'L':
|
||||
d = d.TurnL()
|
||||
case 'R':
|
||||
d = d.TurnR()
|
||||
}
|
||||
}
|
||||
|
||||
if x == 0 && y == 0 && d == UP {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
var _ = isRobotBounded
|
||||
60
solutions/3/q399/solution.go
Normal file
60
solutions/3/q399/solution.go
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
package q399
|
||||
|
||||
func calcEquation(equations [][]string, values []float64, queries [][]string) []float64 {
|
||||
seen := make(map[[2]string]float64, len(equations)*8)
|
||||
queue := make([][2]string, 0, len(equations)*8)
|
||||
|
||||
for i := range equations {
|
||||
eq, val := equations[i], values[i]
|
||||
eq1, eq2 := [2]string{eq[0], eq[1]}, [2]string{eq[1], eq[0]}
|
||||
seen[eq1] = val
|
||||
seen[eq2] = 1 / val
|
||||
queue = append(queue, eq1, eq2)
|
||||
|
||||
seen[[2]string{eq[0], eq[0]}] = 1
|
||||
seen[[2]string{eq[1], eq[1]}] = 1
|
||||
}
|
||||
|
||||
for ; len(queue) > 0; queue = queue[1:] {
|
||||
eq := queue[0]
|
||||
val := seen[eq]
|
||||
|
||||
for i := range equations {
|
||||
var newVar string
|
||||
newVal := val
|
||||
switch eq[0] {
|
||||
case equations[i][0]:
|
||||
newVar = equations[i][1]
|
||||
newVal *= 1 / values[i]
|
||||
case equations[i][1]:
|
||||
newVar = equations[i][0]
|
||||
newVal *= values[i]
|
||||
default:
|
||||
continue
|
||||
}
|
||||
|
||||
newEq1 := [2]string{newVar, eq[1]}
|
||||
if _, ok := seen[newEq1]; !ok {
|
||||
seen[newEq1] = newVal
|
||||
queue = append(queue, newEq1)
|
||||
}
|
||||
newEq2 := [2]string{eq[1], newVar}
|
||||
if _, ok := seen[newEq2]; !ok {
|
||||
seen[newEq2] = 1 / newVal
|
||||
queue = append(queue, newEq2)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ret := make([]float64, len(queries))
|
||||
for i := range queries {
|
||||
if val, ok := seen[[2]string{queries[i][0], queries[i][1]}]; ok {
|
||||
ret[i] = val
|
||||
} else {
|
||||
ret[i] = -1
|
||||
}
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
var _ = calcEquation
|
||||
35
solutions/37/q3713/solution.go
Normal file
35
solutions/37/q3713/solution.go
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
package q3713
|
||||
|
||||
func longestBalanced(s string) int {
|
||||
longest := 0
|
||||
cnt, tmp := [26]int{}, [26]int{}
|
||||
|
||||
for i := range len(s) {
|
||||
copy(cnt[:], tmp[:])
|
||||
distinct, imbal, maxCnt := 0, 0, 0
|
||||
|
||||
for j := i; j >= 0; j-- {
|
||||
c := s[j] - 'a'
|
||||
cnt[c]++
|
||||
if cnt[c] == 1 {
|
||||
distinct++
|
||||
imbal++
|
||||
}
|
||||
|
||||
if cnt[c] > maxCnt {
|
||||
maxCnt = cnt[c]
|
||||
imbal = distinct - 1
|
||||
} else if cnt[c] == maxCnt {
|
||||
imbal--
|
||||
}
|
||||
|
||||
if imbal == 0 {
|
||||
longest = max(longest, i-j+1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return longest
|
||||
}
|
||||
|
||||
var _ = longestBalanced
|
||||
88
solutions/37/q3714/solution.go
Normal file
88
solutions/37/q3714/solution.go
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
package q3714
|
||||
|
||||
func longestBalanced(s string) int {
|
||||
longest := 0
|
||||
single := 0
|
||||
diff3 := make(map[[2]int]int, len(s))
|
||||
diffAB, diffAC := 0, 0
|
||||
|
||||
diff2AB, diff2AC, diff2BC := map[int]int{}, map[int]int{}, map[int]int{}
|
||||
nDiff2AB, nDiff2AC, nDiff2BC := 0, 0, 0
|
||||
lastA, lastB, lastC := -1, -1, -1
|
||||
|
||||
for i := range len(s) {
|
||||
// Single
|
||||
if i > 0 && s[i] == s[i-1] {
|
||||
single++
|
||||
} else {
|
||||
single = 1
|
||||
}
|
||||
longest = max(longest, single)
|
||||
|
||||
// Dual
|
||||
var process2pair = func(diffIdx map[int]int, offset, i, diff int) int {
|
||||
if diff == 0 {
|
||||
return i - offset
|
||||
}
|
||||
if prev, ok := diffIdx[diff]; !ok {
|
||||
diffIdx[diff] = i
|
||||
return 0
|
||||
} else {
|
||||
return i - prev
|
||||
}
|
||||
}
|
||||
switch s[i] {
|
||||
case 'a':
|
||||
lastA = i
|
||||
nDiff2BC, diff2BC = 0, map[int]int{}
|
||||
nDiff2AB++
|
||||
nDiff2AC++
|
||||
t1 := process2pair(diff2AB, lastC, i, nDiff2AB)
|
||||
t2 := process2pair(diff2AC, lastB, i, nDiff2AC)
|
||||
longest = max(longest, t1, t2)
|
||||
|
||||
case 'b':
|
||||
lastB = i
|
||||
nDiff2AC, diff2AC = 0, map[int]int{}
|
||||
nDiff2AB--
|
||||
nDiff2BC++
|
||||
t1 := process2pair(diff2AB, lastC, i, nDiff2AB)
|
||||
t2 := process2pair(diff2BC, lastA, i, nDiff2BC)
|
||||
longest = max(longest, t1, t2)
|
||||
|
||||
case 'c':
|
||||
lastC = i
|
||||
nDiff2AB, diff2AB = 0, map[int]int{}
|
||||
nDiff2BC--
|
||||
nDiff2AC--
|
||||
t1 := process2pair(diff2BC, lastA, i, nDiff2BC)
|
||||
t2 := process2pair(diff2AC, lastB, i, nDiff2AC)
|
||||
longest = max(longest, t1, t2)
|
||||
}
|
||||
|
||||
// Triple
|
||||
switch s[i] {
|
||||
case 'a':
|
||||
diffAB++
|
||||
diffAC++
|
||||
case 'b':
|
||||
diffAB--
|
||||
case 'c':
|
||||
diffAC--
|
||||
}
|
||||
key := [2]int{diffAB, diffAC}
|
||||
if key == [2]int{0, 0} {
|
||||
longest = i + 1
|
||||
continue
|
||||
}
|
||||
if prev, ok := diff3[key]; !ok {
|
||||
diff3[key] = i
|
||||
} else {
|
||||
longest = max(longest, i-prev)
|
||||
}
|
||||
}
|
||||
|
||||
return longest
|
||||
}
|
||||
|
||||
var _ = longestBalanced
|
||||
53
solutions/37/q3719/solution.go
Normal file
53
solutions/37/q3719/solution.go
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
package q3719
|
||||
|
||||
var seen [6000]bool
|
||||
|
||||
func longestBalanced(nums []int) int {
|
||||
// Map numbers to reduce the range
|
||||
mapped := make(map[int]int, len(nums))
|
||||
nextEven, nextOdd := 0, 1
|
||||
for i := range nums {
|
||||
if m, ok := mapped[nums[i]]; ok {
|
||||
nums[i] = m
|
||||
} else if nums[i]%2 == 0 {
|
||||
mapped[nums[i]] = nextEven
|
||||
nums[i] = nextEven
|
||||
nextEven += 2
|
||||
} else {
|
||||
mapped[nums[i]] = nextOdd
|
||||
nums[i] = nextOdd
|
||||
nextOdd += 2
|
||||
}
|
||||
}
|
||||
numRange := max(nextEven, nextOdd)
|
||||
copy(seen[:3000], seen[3000:]) // init
|
||||
|
||||
longest := 0
|
||||
for i := 1; i < len(nums); i++ {
|
||||
copy(seen[:numRange], seen[numRange:]) // reset
|
||||
odd, even := 0, 0
|
||||
for j := i; j >= 0; j-- {
|
||||
num := nums[j]
|
||||
if seen[num] {
|
||||
if even == odd {
|
||||
longest = max(longest, i-j+1)
|
||||
}
|
||||
continue
|
||||
}
|
||||
seen[num] = true
|
||||
|
||||
if num%2 == 0 {
|
||||
even++
|
||||
} else {
|
||||
odd++
|
||||
}
|
||||
if even == odd {
|
||||
longest = max(longest, i-j+1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return longest
|
||||
}
|
||||
|
||||
var _ = longestBalanced
|
||||
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