add new solutions

This commit is contained in:
kanna5 2025-12-29 12:16:39 +09:00
parent 59b71480d4
commit 71189b61cf
Signed by: kkyy
GPG key ID: 06332F3965E9B0CF
8 changed files with 200 additions and 0 deletions

View file

@ -0,0 +1,19 @@
package q14
func longestCommonPrefix(strs []string) string {
minLen := len(strs[0])
for i := 1; i < len(strs); i++ {
minLen = min(minLen, len(strs[i]))
}
for pfxLen := range minLen {
for i := 1; i < len(strs); i++ {
if strs[i][pfxLen] != strs[0][pfxLen] {
return strs[0][:pfxLen]
}
}
}
return strs[0][:minLen]
}
var _ = longestCommonPrefix

View file

@ -0,0 +1,9 @@
package q28
import "strings"
func strStr(haystack string, needle string) int {
return strings.Index(haystack, needle)
}
var _ = strStr

View file

@ -0,0 +1,13 @@
package q58
func lengthOfLastWord(s string) int {
i := len(s)
for ; s[i-1] == ' '; i-- {
}
j := i - 1
for ; j > 0 && s[j-1] != ' '; j-- {
}
return i - j
}
var _ = lengthOfLastWord

View file

@ -0,0 +1,29 @@
package q202
type void struct{}
func next(n int) int {
ret := 0
for n > 0 {
digit := n % 10
n /= 10
ret += digit * digit
}
return ret
}
func isHappy(n int) bool {
seen := map[int]void{n: {}}
for {
n = next(n)
if n == 1 {
return true
}
if _, ok := seen[n]; ok {
return false
}
seen[n] = void{}
}
}
var _ = isHappy

View file

@ -0,0 +1,37 @@
package q222
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func count(node *TreeNode, depth int, maxDepth, mdFill *int) bool {
if node == nil {
return false
}
if node.Left == nil && node.Right == nil { // leaf
switch {
case depth == *maxDepth:
*mdFill++
case depth > *maxDepth:
*maxDepth = depth
*mdFill = 1
case depth < *maxDepth:
return true
}
}
return count(node.Left, depth+1, maxDepth, mdFill) ||
count(node.Right, depth+1, maxDepth, mdFill)
}
func countNodes(root *TreeNode) int {
maxdepth, mdFill := 0, 0
count(root, 1, &maxdepth, &mdFill)
if maxdepth == 0 {
return 0
}
return 1<<(maxdepth-1) - 1 + mdFill
}
var _ = countNodes

View file

@ -0,0 +1,17 @@
package q226
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func invertTree(root *TreeNode) *TreeNode {
if root == nil {
return nil
}
root.Left, root.Right = root.Right, root.Left
invertTree(root.Left)
invertTree(root.Right)
return root
}

View file

@ -0,0 +1,21 @@
package q242
func isAnagram(s string, t string) bool {
if len(s) != len(t) {
return false
}
cnt := map[rune]int{}
for _, r := range s {
cnt[r]++
}
for _, r := range t {
cnt[r]--
if cnt[r] < 0 {
return false
}
}
return true
}
var _ = isAnagram

View file

@ -0,0 +1,55 @@
package q756
type Bottom [2]byte
func canBuild(buffer []byte, layer, offset int, allowed map[Bottom][]byte) bool {
layerOffset := len(buffer) - (layer+1)*layer/2
current := buffer[layerOffset : layerOffset+layer]
bottom := buffer[layerOffset-layer-1 : layerOffset]
nextLayer, nextOffset := layer, offset+1
if nextOffset == layer {
nextLayer, nextOffset = layer-1, 0
}
if current[offset] != 0 { // skip
return canBuild(buffer, nextLayer, nextOffset, allowed)
}
choices := allowed[Bottom{bottom[offset], bottom[offset+1]}]
if len(choices) == 0 {
return false
}
if layer == 1 {
return true
}
next := buffer[layerOffset+layer:]
for _, c := range choices {
current[offset] = c
if offset > 0 {
next[offset-1] = 0 // needs recalculate
}
if offset < layer-1 {
next[offset] = 0
}
if canBuild(buffer, nextLayer, nextOffset, allowed) {
return true
}
}
return false
}
func pyramidTransition(bottom string, allowed []string) bool {
allowedIdx := map[Bottom][]byte{}
for i := range allowed {
k := [2]byte{allowed[i][0], allowed[i][1]}
allowedIdx[k] = append(allowedIdx[k], allowed[i][2])
}
buffer := make([]byte, (len(bottom)+1)*len(bottom)/2)
copy(buffer, []byte(bottom))
return canBuild(buffer, len(bottom)-1, 0, allowedIdx)
}
var _ = pyramidTransition