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