mirror of
https://git.wolves.top/wolves/leetcode.git
synced 2025-11-04 17:26:32 +08:00
init
This commit is contained in:
40
25/05/2131.go
Normal file
40
25/05/2131.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package main
|
||||
|
||||
func Reverse(s string) string {
|
||||
bytes := []byte(s) // 直接转为字节切片
|
||||
for i, j := 0, len(bytes)-1; i < j; i, j = i+1, j-1 {
|
||||
bytes[i], bytes[j] = bytes[j], bytes[i] // 交换字节
|
||||
}
|
||||
return string(bytes)
|
||||
}
|
||||
|
||||
func longestPalindrome(words []string) int {
|
||||
m := make(map[string]int)
|
||||
res, center := 0, 0
|
||||
for _, word := range words {
|
||||
m[word]++
|
||||
}
|
||||
for word := range m {
|
||||
if m[word] == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
reversed := Reverse(word)
|
||||
|
||||
if reversed == word {
|
||||
pairs := m[word] / 2
|
||||
res += pairs * 2 * 2
|
||||
if m[word]%2 == 1 {
|
||||
center = 2
|
||||
}
|
||||
} else {
|
||||
if m[reversed] >= 1 {
|
||||
pairs := min(m[word], m[reversed])
|
||||
res += pairs * 4
|
||||
m[word] -= pairs
|
||||
m[reversed] -= pairs
|
||||
}
|
||||
}
|
||||
}
|
||||
return res + center
|
||||
}
|
||||
9
25/05/2894.go
Normal file
9
25/05/2894.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package main
|
||||
|
||||
func differenceOfSums(n int, m int) int {
|
||||
nums2 := 0
|
||||
for i := 0; i*m <= n; i++ {
|
||||
nums2 += i * m
|
||||
}
|
||||
return n*(n+1)/2 - nums2*2
|
||||
}
|
||||
13
25/05/2942.go
Normal file
13
25/05/2942.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package main
|
||||
|
||||
import "strings"
|
||||
|
||||
func findWordsContaining(words []string, x byte) []int {
|
||||
var result []int
|
||||
for i, word := range words {
|
||||
if strings.Index(word, string(x)) != -1 {
|
||||
result = append(result, i)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
Reference in New Issue
Block a user