add new solutions
This commit is contained in:
parent
475d438db4
commit
1433bf4850
17 changed files with 394 additions and 0 deletions
39
solutions/0/q17/solution.go
Normal file
39
solutions/0/q17/solution.go
Normal file
|
|
@ -0,0 +1,39 @@
|
||||||
|
package q17
|
||||||
|
|
||||||
|
var alphabets = [][]byte{
|
||||||
|
[]byte("abc"),
|
||||||
|
[]byte("def"),
|
||||||
|
[]byte("ghi"),
|
||||||
|
[]byte("jkl"),
|
||||||
|
[]byte("mno"),
|
||||||
|
[]byte("pqrs"),
|
||||||
|
[]byte("tuv"),
|
||||||
|
[]byte("wxyz"),
|
||||||
|
}
|
||||||
|
|
||||||
|
func combinations(offsets []int, i int, buffer []byte, strings []string) []string {
|
||||||
|
if buffer == nil {
|
||||||
|
buffer = make([]byte, len(offsets))
|
||||||
|
}
|
||||||
|
|
||||||
|
if i == len(offsets) {
|
||||||
|
return append(strings, string(buffer))
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, c := range alphabets[offsets[i]] {
|
||||||
|
buffer[i] = c
|
||||||
|
strings = combinations(offsets, i+1, buffer, strings)
|
||||||
|
}
|
||||||
|
return strings
|
||||||
|
}
|
||||||
|
|
||||||
|
func letterCombinations(digits string) []string {
|
||||||
|
offsets := make([]int, len(digits))
|
||||||
|
for i := range len(digits) {
|
||||||
|
offsets[i] = int(digits[i] - '2')
|
||||||
|
}
|
||||||
|
|
||||||
|
return combinations(offsets, 0, nil, []string{})
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ = letterCombinations
|
||||||
19
solutions/0/q35/solution.go
Normal file
19
solutions/0/q35/solution.go
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
package q35
|
||||||
|
|
||||||
|
func searchInsert(nums []int, target int) int {
|
||||||
|
l, r := 0, len(nums)
|
||||||
|
for l < r {
|
||||||
|
mid := (l + r) / 2
|
||||||
|
switch {
|
||||||
|
case nums[mid] == target:
|
||||||
|
return mid
|
||||||
|
case nums[mid] > target:
|
||||||
|
r = mid
|
||||||
|
default:
|
||||||
|
l = mid + 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return l
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ = searchInsert
|
||||||
18
solutions/0/q53/solution.go
Normal file
18
solutions/0/q53/solution.go
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
package q53
|
||||||
|
|
||||||
|
import "math"
|
||||||
|
|
||||||
|
func maxSubArray(nums []int) int {
|
||||||
|
sum := 0
|
||||||
|
minSubstract := 0
|
||||||
|
maxSubArr := math.MinInt
|
||||||
|
for i := range nums {
|
||||||
|
sum += nums[i]
|
||||||
|
nums[i] = sum
|
||||||
|
maxSubArr = max(maxSubArr, nums[i]-minSubstract)
|
||||||
|
minSubstract = min(minSubstract, nums[i])
|
||||||
|
}
|
||||||
|
return maxSubArr
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ = maxSubArray
|
||||||
22
solutions/0/q66/solution.go
Normal file
22
solutions/0/q66/solution.go
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
package q66
|
||||||
|
|
||||||
|
func plusOne(digits []int) []int {
|
||||||
|
digits[len(digits)-1]++
|
||||||
|
for i := len(digits) - 1; i > 0; i-- {
|
||||||
|
if digits[i] > 9 {
|
||||||
|
digits[i] -= 10
|
||||||
|
digits[i-1]++
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if digits[0] > 9 {
|
||||||
|
digits[0] -= 10
|
||||||
|
ret := make([]int, 0, len(digits)+1)
|
||||||
|
ret = append(ret, 1)
|
||||||
|
return append(ret, digits...)
|
||||||
|
}
|
||||||
|
return digits
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ = plusOne
|
||||||
28
solutions/0/q67/solution.go
Normal file
28
solutions/0/q67/solution.go
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
package q67
|
||||||
|
|
||||||
|
func toDigit(byt byte) uint8 { return byt - '0' }
|
||||||
|
func toByte(digit uint8) byte { return '0' + digit }
|
||||||
|
|
||||||
|
func addBinary(a string, b string) string {
|
||||||
|
lenA, lenB := len(a), len(b)
|
||||||
|
ret := make([]byte, max(lenA, lenB)+1)
|
||||||
|
var carry uint8
|
||||||
|
for i := range ret {
|
||||||
|
d := carry
|
||||||
|
if i < lenA {
|
||||||
|
d += toDigit(a[lenA-i-1])
|
||||||
|
}
|
||||||
|
if i < lenB {
|
||||||
|
d += toDigit(b[lenB-i-1])
|
||||||
|
}
|
||||||
|
carry = d / 2
|
||||||
|
ret[len(ret)-i-1] = toByte(d % 2)
|
||||||
|
}
|
||||||
|
|
||||||
|
if ret[0] != '1' {
|
||||||
|
return string(ret[1:])
|
||||||
|
}
|
||||||
|
return string(ret)
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ = addBinary
|
||||||
26
solutions/0/q69/solution.go
Normal file
26
solutions/0/q69/solution.go
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
package q69
|
||||||
|
|
||||||
|
func mySqrt(x int) int {
|
||||||
|
if x < 2 {
|
||||||
|
return x
|
||||||
|
}
|
||||||
|
l, r := 0, x/2+1
|
||||||
|
for l < r {
|
||||||
|
m := (l + r) / 2
|
||||||
|
sq := m * m
|
||||||
|
switch {
|
||||||
|
case sq == x:
|
||||||
|
return m
|
||||||
|
case sq < x:
|
||||||
|
if (m+1)*(m+1) > x {
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
l = m + 1
|
||||||
|
case sq > x:
|
||||||
|
r = m
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ = mySqrt
|
||||||
24
solutions/0/q70/solution.go
Normal file
24
solutions/0/q70/solution.go
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
package q70
|
||||||
|
|
||||||
|
func nWays(n, i int, cache []int) int {
|
||||||
|
if i == n {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
if i > n {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
if cache == nil {
|
||||||
|
cache = make([]int, n)
|
||||||
|
} else if cache[i] != 0 {
|
||||||
|
return cache[i]
|
||||||
|
}
|
||||||
|
ret := nWays(n, i+1, cache) + nWays(n, i+2, cache)
|
||||||
|
cache[i] = ret
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
func climbStairs(n int) int {
|
||||||
|
return nWays(n, 0, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ = climbStairs
|
||||||
18
solutions/0/q9/solution.go
Normal file
18
solutions/0/q9/solution.go
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
package q9
|
||||||
|
|
||||||
|
import "strconv"
|
||||||
|
|
||||||
|
func isPalindrome(x int) bool {
|
||||||
|
if x < 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
str := strconv.FormatInt(int64(x), 10)
|
||||||
|
for i := range len(str) / 2 {
|
||||||
|
if str[i] != str[len(str)-1-i] {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ = isPalindrome
|
||||||
26
solutions/1/q108/solution.go
Normal file
26
solutions/1/q108/solution.go
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
package q108
|
||||||
|
|
||||||
|
type TreeNode struct {
|
||||||
|
Val int
|
||||||
|
Left *TreeNode
|
||||||
|
Right *TreeNode
|
||||||
|
}
|
||||||
|
|
||||||
|
func sortedArrayToBST(nums []int) *TreeNode {
|
||||||
|
if len(nums) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if len(nums) == 1 {
|
||||||
|
return &TreeNode{Val: nums[0]}
|
||||||
|
}
|
||||||
|
|
||||||
|
l, r := nums[:len(nums)/2], nums[len(nums)/2+1:]
|
||||||
|
m := nums[len(nums)/2]
|
||||||
|
return &TreeNode{
|
||||||
|
Val: m,
|
||||||
|
Left: sortedArrayToBST(l),
|
||||||
|
Right: sortedArrayToBST(r),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ = sortedArrayToBST
|
||||||
14
solutions/1/q120/solution.go
Normal file
14
solutions/1/q120/solution.go
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
package q120
|
||||||
|
|
||||||
|
func minimumTotal(triangle [][]int) int {
|
||||||
|
size := len(triangle)
|
||||||
|
|
||||||
|
for row := size - 2; row >= 0; row-- {
|
||||||
|
for i := range triangle[row] {
|
||||||
|
triangle[row][i] += min(triangle[row+1][i], triangle[row+1][i+1])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return triangle[0][0]
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ = minimumTotal
|
||||||
11
solutions/1/q136/solution.go
Normal file
11
solutions/1/q136/solution.go
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
package q136
|
||||||
|
|
||||||
|
func singleNumber(nums []int) int {
|
||||||
|
x := 0
|
||||||
|
for _, num := range nums {
|
||||||
|
x ^= num
|
||||||
|
}
|
||||||
|
return x
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ = singleNumber
|
||||||
13
solutions/1/q190/solution.go
Normal file
13
solutions/1/q190/solution.go
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
package q190
|
||||||
|
|
||||||
|
func reverseBits(n int) int {
|
||||||
|
ret := 0
|
||||||
|
for i := range 32 {
|
||||||
|
if n|(1<<i) == n {
|
||||||
|
ret |= 1 << (31 - i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ = reverseBits
|
||||||
13
solutions/1/q191/solution.go
Normal file
13
solutions/1/q191/solution.go
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
package q191
|
||||||
|
|
||||||
|
func hammingWeight(n int) int {
|
||||||
|
num := 0
|
||||||
|
for ; n > 0; n >>= 1 {
|
||||||
|
if n|1 == n {
|
||||||
|
num++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return num
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ = hammingWeight
|
||||||
37
solutions/2/q215/solution.go
Normal file
37
solutions/2/q215/solution.go
Normal file
|
|
@ -0,0 +1,37 @@
|
||||||
|
package q215
|
||||||
|
|
||||||
|
func findKthLargest(nums []int, k int) int {
|
||||||
|
// build max-heap
|
||||||
|
for i := 1; i < len(nums); i++ {
|
||||||
|
for j := i; j > 0; j = (j - 1) / 2 {
|
||||||
|
parent := (j - 1) / 2
|
||||||
|
if nums[j] > nums[parent] {
|
||||||
|
nums[j], nums[parent] = nums[parent], nums[j]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for range k - 1 {
|
||||||
|
// pop the heap
|
||||||
|
nums[0] = nums[len(nums)-1]
|
||||||
|
nums = nums[:len(nums)-1]
|
||||||
|
|
||||||
|
i := 0
|
||||||
|
for i*2+1 < len(nums) {
|
||||||
|
l, r := i*2+1, i*2+2
|
||||||
|
next := l
|
||||||
|
if r < len(nums) && nums[r] > nums[l] {
|
||||||
|
next = r
|
||||||
|
}
|
||||||
|
if nums[i] >= nums[next] {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
nums[i], nums[next] = nums[next], nums[i]
|
||||||
|
i = next
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nums[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ = findKthLargest
|
||||||
29
solutions/24/q2483/solution.go
Normal file
29
solutions/24/q2483/solution.go
Normal file
|
|
@ -0,0 +1,29 @@
|
||||||
|
package q2483
|
||||||
|
|
||||||
|
func bestClosingTime(customers string) int {
|
||||||
|
numY := 0
|
||||||
|
for i := range len(customers) {
|
||||||
|
if customers[i] == 'Y' {
|
||||||
|
numY++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
minCloseHour := 0
|
||||||
|
minPenalty := numY
|
||||||
|
penalty := numY
|
||||||
|
for i := range len(customers) {
|
||||||
|
// close at (i + 1)th hour
|
||||||
|
switch customers[i] {
|
||||||
|
case 'N':
|
||||||
|
penalty++
|
||||||
|
case 'Y':
|
||||||
|
penalty--
|
||||||
|
}
|
||||||
|
if penalty < minPenalty {
|
||||||
|
minPenalty = penalty
|
||||||
|
minCloseHour = i + 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return minCloseHour
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ = bestClosingTime
|
||||||
14
solutions/30/q3075/solution.go
Normal file
14
solutions/30/q3075/solution.go
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
package q3075
|
||||||
|
|
||||||
|
import "slices"
|
||||||
|
|
||||||
|
func maximumHappinessSum(happiness []int, k int) int64 {
|
||||||
|
slices.Sort(happiness)
|
||||||
|
var sum int64
|
||||||
|
for i := range k {
|
||||||
|
sum += max(0, int64(happiness[len(happiness)-1-i]-i))
|
||||||
|
}
|
||||||
|
return sum
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ = maximumHappinessSum
|
||||||
43
solutions/6/q637/solution.go
Normal file
43
solutions/6/q637/solution.go
Normal file
|
|
@ -0,0 +1,43 @@
|
||||||
|
package q637
|
||||||
|
|
||||||
|
type TreeNode struct {
|
||||||
|
Val int
|
||||||
|
Left *TreeNode
|
||||||
|
Right *TreeNode
|
||||||
|
}
|
||||||
|
|
||||||
|
func averageOfLevels(root *TreeNode) []float64 {
|
||||||
|
type qElem struct {
|
||||||
|
level int
|
||||||
|
node *TreeNode
|
||||||
|
}
|
||||||
|
|
||||||
|
ret := []float64{}
|
||||||
|
level := 0
|
||||||
|
n := 0 // number of nodes
|
||||||
|
var sum float64
|
||||||
|
|
||||||
|
queue := []qElem{{level: 0, node: root}}
|
||||||
|
for ; len(queue) > 0; queue = queue[1:] {
|
||||||
|
curr := &queue[0]
|
||||||
|
if curr.level == level {
|
||||||
|
sum += float64(curr.node.Val)
|
||||||
|
n++
|
||||||
|
} else {
|
||||||
|
ret = append(ret, sum/float64(n))
|
||||||
|
sum = float64(curr.node.Val)
|
||||||
|
n = 1
|
||||||
|
level = curr.level
|
||||||
|
}
|
||||||
|
if curr.node.Left != nil {
|
||||||
|
queue = append(queue, qElem{curr.level + 1, curr.node.Left})
|
||||||
|
}
|
||||||
|
if curr.node.Right != nil {
|
||||||
|
queue = append(queue, qElem{curr.level + 1, curr.node.Right})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ret = append(ret, sum/float64(n))
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ = averageOfLevels
|
||||||
Loading…
Add table
Add a link
Reference in a new issue