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
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										45
									
								
								25/06/1061.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								25/06/1061.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,45 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
type UnionFind struct {
 | 
			
		||||
	parent []int
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewUnionFind(n int) *UnionFind {
 | 
			
		||||
	uf := &UnionFind{parent: make([]int, n)}
 | 
			
		||||
	for i := 0; i < n; i++ {
 | 
			
		||||
		uf.parent[i] = i
 | 
			
		||||
	}
 | 
			
		||||
	return uf
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (uf *UnionFind) Find(x int) int {
 | 
			
		||||
	if uf.parent[x] != x {
 | 
			
		||||
		uf.parent[x] = uf.Find(uf.parent[x])
 | 
			
		||||
	}
 | 
			
		||||
	return uf.parent[x]
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (uf *UnionFind) Unite(x, y int) {
 | 
			
		||||
	x, y = uf.Find(x), uf.Find(y)
 | 
			
		||||
	if x == y {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	if x > y {
 | 
			
		||||
		x, y = y, x
 | 
			
		||||
	}
 | 
			
		||||
	// 总是让字典序更小的作为集合代表字符
 | 
			
		||||
	uf.parent[y] = x
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func smallestEquivalentString(s1 string, s2 string, baseStr string) string {
 | 
			
		||||
	uf := NewUnionFind(26)
 | 
			
		||||
	for i := 0; i < len(s1); i++ {
 | 
			
		||||
		uf.Unite(int(s1[i]-'a'), int(s2[i]-'a'))
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	res := []byte(baseStr)
 | 
			
		||||
	for i := range res {
 | 
			
		||||
		res[i] = byte('a' + uf.Find(int(res[i]-'a')))
 | 
			
		||||
	}
 | 
			
		||||
	return string(res)
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										38
									
								
								25/06/1432.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								25/06/1432.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,38 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"strconv"
 | 
			
		||||
	"strings"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func maxDiff(num int) int {
 | 
			
		||||
	replace := func(s string, x, y rune) string {
 | 
			
		||||
		return strings.ReplaceAll(s, string(x), string(y))
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	num_max := strconv.Itoa(num)
 | 
			
		||||
	num_min := num_max
 | 
			
		||||
 | 
			
		||||
	for _, c := range num_max {
 | 
			
		||||
		if c != '9' {
 | 
			
		||||
			num_max = replace(num_max, c, '9')
 | 
			
		||||
			break
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	for i, c := range num_min {
 | 
			
		||||
		if i == 0 && c != '1' {
 | 
			
		||||
			num_min = replace(num_min, c, '1')
 | 
			
		||||
			break
 | 
			
		||||
		}
 | 
			
		||||
		if c != '0' && c != rune(num_min[0]) {
 | 
			
		||||
			num_min = replace(num_min, c, '0')
 | 
			
		||||
			break
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	x, _ := strconv.Atoi(num_max)
 | 
			
		||||
	y, _ := strconv.Atoi(num_min)
 | 
			
		||||
 | 
			
		||||
	return x - y
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										13
									
								
								25/06/2929.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								25/06/2929.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,13 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
func distributeCandies(n int, limit int) int64 {
 | 
			
		||||
	var res int64 = 0
 | 
			
		||||
	for i := 0; i <= min(n, limit); i++ {
 | 
			
		||||
		last := n - i
 | 
			
		||||
		if last > 2*limit {
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
		res += int64(min(last, limit) - max(0, last-limit) + 1)
 | 
			
		||||
	}
 | 
			
		||||
	return res
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										12
									
								
								25/06/3423.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								25/06/3423.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,12 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import "math"
 | 
			
		||||
 | 
			
		||||
func maxAdjacentDistance(nums []int) int {
 | 
			
		||||
	n := len(nums)
 | 
			
		||||
	max := int(math.Abs(float64(nums[0] - nums[n-1])))
 | 
			
		||||
	for i := 0; i < n-1; i++ {
 | 
			
		||||
		max = int(math.Max(float64(max), float64(math.Abs(float64(nums[i]-nums[i+1])))))
 | 
			
		||||
	}
 | 
			
		||||
	return max
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										19
									
								
								25/06/3442.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								25/06/3442.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,19 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
func maxDifference(s string) int {
 | 
			
		||||
	m := make(map[rune]int)
 | 
			
		||||
 | 
			
		||||
	maxOdd := 0
 | 
			
		||||
	minEven := len(s)
 | 
			
		||||
	for _, c := range s {
 | 
			
		||||
		m[c]++
 | 
			
		||||
	}
 | 
			
		||||
	for _, v := range m {
 | 
			
		||||
		if v%2 == 1 {
 | 
			
		||||
			maxOdd = max(maxOdd, v)
 | 
			
		||||
		} else {
 | 
			
		||||
			minEven = min(minEven, v)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return maxOdd - minEven
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										18
									
								
								25/06/386.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								25/06/386.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,18 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
func lexicalOrder(n int) []int {
 | 
			
		||||
	ans := make([]int, n)
 | 
			
		||||
	num := 1
 | 
			
		||||
	for i := 0; i < n; i++ {
 | 
			
		||||
		ans[i] = num
 | 
			
		||||
		if num*10 <= n {
 | 
			
		||||
			num *= 10
 | 
			
		||||
		} else {
 | 
			
		||||
			for num%10 == 9 || num+1 > n {
 | 
			
		||||
				num /= 10
 | 
			
		||||
			}
 | 
			
		||||
			num++
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return ans
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user