mirror of
https://github.com/lWolvesl/leetcode.git
synced 2026-07-18 03:12:22 +08:00
Compare commits
16
Commits
4180df8390
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bb44fe2957 | ||
|
|
39954c4a80 | ||
|
|
4355946f39 | ||
|
|
45faf83d37 | ||
|
|
6339943b70 | ||
|
|
be9ecb423a | ||
|
|
84921163ec | ||
|
|
9b63dc45d3 | ||
|
|
548ca1ffc2 | ||
|
|
de39ce466a | ||
|
|
dbfb9704f3 | ||
|
|
558533cd01 | ||
|
|
ed9ac364a8 | ||
|
|
035120f931 | ||
|
|
62acda6cf7 | ||
|
|
3c8405abf2 |
@@ -1,11 +0,0 @@
|
|||||||
package A
|
|
||||||
|
|
||||||
func minCost(n int, edges [][]int) int {
|
|
||||||
edgeM := make([][]int, n)
|
|
||||||
for i := 0; i < n; i++ {
|
|
||||||
edgeM[i] = make([]int, n)
|
|
||||||
}
|
|
||||||
for _, e := range edges {
|
|
||||||
edgeM[e[0]][e[1]] = e[2]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math/bits"
|
||||||
|
)
|
||||||
|
|
||||||
|
func bitwiseComplement(n int) int {
|
||||||
|
if n == 0 {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
return n ^ (1<<bits.Len(uint(n)) - 1)
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "math"
|
||||||
|
|
||||||
|
func getHappyString(n int, k int) string {
|
||||||
|
bt := int(math.Pow(2.0, float64(n-1)))
|
||||||
|
maxK := 3 * bt
|
||||||
|
if k > maxK {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
slice := []byte{'a', 'b', 'c'}
|
||||||
|
sl := [][]byte{{'b', 'c'}, {'a', 'c'}, {'a', 'b'}}
|
||||||
|
res := make([]byte, 0, n)
|
||||||
|
if k <= bt {
|
||||||
|
res = append(res, slice[0])
|
||||||
|
bt = 0
|
||||||
|
} else if k > bt && k <= 2*bt {
|
||||||
|
res = append(res, slice[1])
|
||||||
|
} else {
|
||||||
|
res = append(res, slice[2])
|
||||||
|
bt *= 2
|
||||||
|
}
|
||||||
|
for i := 1; i < n; i++ {
|
||||||
|
temp := res[i-1]
|
||||||
|
i1 := int(temp - 'a')
|
||||||
|
bti := int(math.Pow(2.0, float64(n-i-1)))
|
||||||
|
if k <= bt+bti {
|
||||||
|
res = append(res, sl[i1][0])
|
||||||
|
} else {
|
||||||
|
res = append(res, sl[i1][1])
|
||||||
|
bt += bti
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return string(res)
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"slices"
|
||||||
|
)
|
||||||
|
|
||||||
|
var cache = []byte("0")
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
for range 20 {
|
||||||
|
temp := append([]byte(nil), cache...)
|
||||||
|
for i := range len(temp) {
|
||||||
|
if temp[i] == '1' {
|
||||||
|
temp[i] = '0'
|
||||||
|
} else {
|
||||||
|
temp[i] = '1'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
slices.Reverse(temp)
|
||||||
|
cache = append(cache, '1')
|
||||||
|
cache = append(cache, temp...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func findKthBit(n int, k int) byte {
|
||||||
|
return cache[k-1]
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
func numSpecial(mat [][]int) int {
|
||||||
|
res := 0
|
||||||
|
x := make(map[int]int)
|
||||||
|
y := make(map[int]int)
|
||||||
|
for i, row := range mat {
|
||||||
|
for j, v := range row {
|
||||||
|
if v == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
x[i]++
|
||||||
|
y[j]++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for i, row := range mat {
|
||||||
|
for j, v := range row {
|
||||||
|
if v == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if x[i] == 1 && y[j] == 1 {
|
||||||
|
res++
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
func minPartitions(n string) int {
|
||||||
|
res := 0
|
||||||
|
for _, v := range n {
|
||||||
|
t := int(v - '0')
|
||||||
|
if res < t {
|
||||||
|
res = t
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
func majorityElement(nums []int) int {
|
||||||
|
a, s := nums[0], 1
|
||||||
|
for i := 1; i < len(nums); i++ {
|
||||||
|
if a == nums[i] {
|
||||||
|
s++
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
s--
|
||||||
|
if s == 0 {
|
||||||
|
a = nums[i]
|
||||||
|
s = 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return a
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
func minOperations(s string) int {
|
||||||
|
a := 0
|
||||||
|
for i := 0; i < len(s); i++ {
|
||||||
|
expected := byte('0' + i%2)
|
||||||
|
if s[i] == expected {
|
||||||
|
a++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
b := len(s) - a
|
||||||
|
if a < b {
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
return b
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "strings"
|
||||||
|
|
||||||
|
func checkOnesSegment(s string) bool {
|
||||||
|
return !strings.Contains(s, "01")
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
func findDifferentBinaryString(nums []string) string {
|
||||||
|
n := len(nums)
|
||||||
|
m := make(map[int]int)
|
||||||
|
mx := 1 << n
|
||||||
|
for _, v := range nums {
|
||||||
|
temp := 0
|
||||||
|
for i, b := range v {
|
||||||
|
if b == '1' {
|
||||||
|
temp += 1 << (n - 1 - i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
m[temp] = 1
|
||||||
|
}
|
||||||
|
for i := range mx {
|
||||||
|
|
||||||
|
if _, ok := m[i]; !ok {
|
||||||
|
s := strconv.FormatInt(int64(i), 2)
|
||||||
|
return fmt.Sprintf("%0*s", n, s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func areSimilar(mat [][]int, k int) bool {
|
||||||
|
lenX := len(mat)
|
||||||
|
lenY := len(mat[0])
|
||||||
|
shift := k % lenY
|
||||||
|
|
||||||
|
for i := 0; i < lenX; i++ {
|
||||||
|
for j := 0; j < lenY; j++ {
|
||||||
|
next := 0
|
||||||
|
if i%2 == 0 {
|
||||||
|
next = (j + shift) % lenY
|
||||||
|
} else {
|
||||||
|
next = (j - shift + lenY) % lenY
|
||||||
|
}
|
||||||
|
if mat[i][j] != mat[i][next] {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println(areSimilar([][]int{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, 4))
|
||||||
|
fmt.Println(areSimilar([][]int{{1, 2, 1, 2}, {5, 5, 5, 5}, {6, 3, 6, 3}}, 2))
|
||||||
|
fmt.Println(areSimilar([][]int{{2, 2}, {2, 2}}, 3))
|
||||||
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func countSubmatrices(grid [][]int, k int) int {
|
||||||
|
m := len(grid)
|
||||||
|
n := len(grid[0])
|
||||||
|
res := 0
|
||||||
|
perfix := make([][]int, m)
|
||||||
|
for i := range perfix {
|
||||||
|
perfix[i] = make([]int, n)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < m; i++ {
|
||||||
|
for j := 0; j < n; j++ {
|
||||||
|
if i == 0 && j == 0 {
|
||||||
|
perfix[i][j] = grid[0][0]
|
||||||
|
} else if i == 0 {
|
||||||
|
perfix[i][j] = perfix[i][j-1] + grid[i][j]
|
||||||
|
} else if j == 0 {
|
||||||
|
perfix[i][j] = perfix[i-1][j] + grid[i][j]
|
||||||
|
} else {
|
||||||
|
perfix[i][j] = perfix[i-1][j] + perfix[i][j-1] - perfix[i-1][j-1] + grid[i][j]
|
||||||
|
}
|
||||||
|
if perfix[i][j] <= k {
|
||||||
|
res++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println(countSubmatrices([][]int{{7, 2, 9}, {1, 5, 0}, {2, 6, 6}}, 18))
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
func reverseSubmatrix(grid [][]int, x int, y int, k int) [][]int {
|
||||||
|
for i := 0; i < k/2; i++ {
|
||||||
|
for j := range k {
|
||||||
|
temp := grid[x+k-1-i][y+j]
|
||||||
|
grid[x+k-1-i][y+j] = grid[x+i][y+j]
|
||||||
|
grid[x+i][y+j] = temp
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return grid
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package leetcode
|
||||||
|
|
||||||
|
import "math"
|
||||||
|
|
||||||
|
func abs(x int) int {
|
||||||
|
if x < 0 {
|
||||||
|
return -x
|
||||||
|
}
|
||||||
|
return x
|
||||||
|
}
|
||||||
|
|
||||||
|
func getMinDistance(nums []int, target int, start int) int {
|
||||||
|
ans := math.MaxInt
|
||||||
|
for i, v := range nums {
|
||||||
|
if v == target {
|
||||||
|
ans = min(abs(i-start), ans)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ans
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
package leetcode
|
||||||
|
|
||||||
|
// 旋转90度通用方法就是 先水平旋转,然后转置
|
||||||
|
func findRotation(mat [][]int, target [][]int) bool {
|
||||||
|
n := len(mat)
|
||||||
|
// 最多旋转 4 次
|
||||||
|
for k := 0; k < 4; k++ {
|
||||||
|
// 旋转操作
|
||||||
|
for i := 0; i < n/2; i++ {
|
||||||
|
for j := 0; j < (n+1)/2; j++ {
|
||||||
|
mat[i][j], mat[n-1-j][i], mat[n-1-i][n-1-j], mat[j][n-1-i] =
|
||||||
|
mat[n-1-j][i], mat[n-1-i][n-1-j], mat[j][n-1-i], mat[i][j]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if isEqual(mat, target) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func isEqual(mat, target [][]int) bool {
|
||||||
|
n := len(mat)
|
||||||
|
for i := 0; i < n; i++ {
|
||||||
|
for j := 0; j < n; j++ {
|
||||||
|
if mat[i][j] != target[i][j] {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
package leetcode
|
||||||
|
|
||||||
|
func abs(x int) int {
|
||||||
|
if x < 0 {
|
||||||
|
return -x
|
||||||
|
}
|
||||||
|
return x
|
||||||
|
}
|
||||||
|
|
||||||
|
func closestTarget(words []string, target string, startIndex int) int {
|
||||||
|
ans := len(words)
|
||||||
|
n := len(words)
|
||||||
|
|
||||||
|
for i, word := range words {
|
||||||
|
if word == target {
|
||||||
|
dist := abs(i - startIndex)
|
||||||
|
ans = min(ans, min(dist, n-dist))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ans < n {
|
||||||
|
return ans
|
||||||
|
}
|
||||||
|
return -1
|
||||||
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
package leetcode
|
||||||
|
|
||||||
|
import "math"
|
||||||
|
|
||||||
|
func abs(x int) int {
|
||||||
|
if x < 0 {
|
||||||
|
return -x
|
||||||
|
}
|
||||||
|
return x
|
||||||
|
}
|
||||||
|
|
||||||
|
func minimumDistance(nums []int) int {
|
||||||
|
m := make(map[int]([]int))
|
||||||
|
for i, v := range nums {
|
||||||
|
_, ok := m[v]
|
||||||
|
if !ok {
|
||||||
|
m[v] = make([]int, 0)
|
||||||
|
}
|
||||||
|
m[v] = append(m[v], i)
|
||||||
|
}
|
||||||
|
ans := math.MaxInt
|
||||||
|
for _, v := range m {
|
||||||
|
if len(v) < 3 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
for i := 0; i+2 < len(v); i++ {
|
||||||
|
temp := abs(v[i]-v[i+1]) + abs(v[i]-v[i+2]) + abs(v[i+1]-v[i+2])
|
||||||
|
ans = min(ans, temp)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ans == math.MaxInt {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
return ans
|
||||||
|
}
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
package A
|
||||||
|
|
||||||
|
type node1559 struct {
|
||||||
|
x, y int
|
||||||
|
px, py int
|
||||||
|
}
|
||||||
|
|
||||||
|
func containsCycle(grid [][]byte) bool {
|
||||||
|
m := len(grid)
|
||||||
|
n := len(grid[0])
|
||||||
|
if m < 2 || n < 2 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
visited := make([][]bool, m)
|
||||||
|
for i := range visited {
|
||||||
|
visited[i] = make([]bool, n)
|
||||||
|
}
|
||||||
|
|
||||||
|
dirs := [][2]int{{0, -1}, {0, 1}, {-1, 0}, {1, 0}}
|
||||||
|
|
||||||
|
for i := 0; i < m; i++ {
|
||||||
|
for j := 0; j < n; j++ {
|
||||||
|
if visited[i][j] {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
visited[i][j] = true
|
||||||
|
stack := []node1559{{x: i, y: j, px: -1, py: -1}}
|
||||||
|
|
||||||
|
for len(stack) > 0 {
|
||||||
|
top := stack[len(stack)-1]
|
||||||
|
stack = stack[:len(stack)-1]
|
||||||
|
|
||||||
|
for _, dir := range dirs {
|
||||||
|
nx := top.x + dir[0]
|
||||||
|
ny := top.y + dir[1]
|
||||||
|
if nx < 0 || nx >= m || ny < 0 || ny >= n {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if grid[nx][ny] != grid[top.x][top.y] {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if !visited[nx][ny] {
|
||||||
|
visited[nx][ny] = true
|
||||||
|
stack = append(stack, node1559{x: nx, y: ny, px: top.x, py: top.y})
|
||||||
|
} else if nx != top.px || ny != top.py {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
@@ -1,71 +0,0 @@
|
|||||||
# CLAUDE.md
|
|
||||||
|
|
||||||
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
|
|
||||||
|
|
||||||
## Project Overview
|
|
||||||
This is a LeetCode solutions repository organized by date and problem type. The codebase contains solutions in multiple languages including C, C++, Go, and Java. The repository is structured with:
|
|
||||||
- Date-based directories (e.g., `23/04/`, `25/11/`) containing individual problem solutions
|
|
||||||
- Data structure implementations in the `dataStruct/` directory (LinkedList, Heap, Tree, etc.)
|
|
||||||
- Special categories like "dynamic planning" and "else"
|
|
||||||
|
|
||||||
## Development Commands
|
|
||||||
|
|
||||||
### Building and Running Code
|
|
||||||
Since this repository contains individual solution files rather than a unified project, compilation and execution is done per file:
|
|
||||||
|
|
||||||
**For C/C++ files:**
|
|
||||||
```bash
|
|
||||||
# Compile C file
|
|
||||||
gcc -o solution filename.c
|
|
||||||
|
|
||||||
# Compile C++ file
|
|
||||||
g++ -o solution filename.cpp
|
|
||||||
|
|
||||||
# Run the compiled executable
|
|
||||||
./solution
|
|
||||||
```
|
|
||||||
|
|
||||||
**For Go files:**
|
|
||||||
```bash
|
|
||||||
# Run Go file directly
|
|
||||||
go run filename.go
|
|
||||||
|
|
||||||
# Build Go executable
|
|
||||||
go build filename.go
|
|
||||||
```
|
|
||||||
|
|
||||||
**For Java files:**
|
|
||||||
```bash
|
|
||||||
# Compile Java file
|
|
||||||
javac filename.java
|
|
||||||
|
|
||||||
# Run Java program
|
|
||||||
java filename
|
|
||||||
```
|
|
||||||
|
|
||||||
### Testing Solutions
|
|
||||||
There are no automated tests in this repository. Each solution should be tested manually by:
|
|
||||||
1. Compiling/running the solution file directly
|
|
||||||
2. Verifying output against expected LeetCode test cases
|
|
||||||
3. Using the data structure helper functions (like `list()`, `len()`, `creatRandomTree()`) for debugging
|
|
||||||
|
|
||||||
## Code Architecture
|
|
||||||
|
|
||||||
### Data Structures
|
|
||||||
The repository includes custom implementations of common data structures:
|
|
||||||
- **LinkedList**: Defined in `dataStruct/LinkedList/lists.h` with `ListNode` struct and utility functions (`createRandomList`, `list`, `array`, `len`)
|
|
||||||
- **Tree**: Defined in `dataStruct/Tree/Tree.h` with `TreeNode` struct and `creatRandomTree` function
|
|
||||||
- **Heap**: Basic template implementation in `dataStruct/Heap/Heap.h`
|
|
||||||
- **Queue**: Priority queue implementation in `dataStruct/Queue/PriorityQueue.c`
|
|
||||||
|
|
||||||
### File Organization
|
|
||||||
- **Date directories**: Solutions organized by year/month (e.g., `25/11/` for November 2025)
|
|
||||||
- **Language variants**: Some problems have multiple language implementations (e.g., `3005.go`, `3005.cpp`, `3005.java`)
|
|
||||||
- **Utility headers**: Common tools and data structures in `tools.h` and the `dataStruct/` directory
|
|
||||||
|
|
||||||
### Common Patterns
|
|
||||||
- Solutions often include debug output functions for visualizing data structures
|
|
||||||
- Random data generation functions are provided for testing (`createRandomList`, `creatRandomTree`)
|
|
||||||
- Header files use proper include guards and pragma once directives
|
|
||||||
|
|
||||||
When working on new solutions, follow the existing patterns and place files in the appropriate date directory based on when the solution was created.
|
|
||||||
Reference in New Issue
Block a user