Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
ruijzhan committed Nov 17, 2024
1 parent f2bf22e commit 09dc742
Show file tree
Hide file tree
Showing 10 changed files with 294 additions and 35 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

| 难度 | 完成 |
| ---- | ----|
| 简单 | 116 |
| 中等 | 60 |
| 简单 | 118 |
| 中等 | 61 |
| 困难 | 4 |
32 changes: 15 additions & 17 deletions gen_report.sh
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
#!/bin/bash

files=$(find . -name *.go| grep -v _test | cut -d'/' -f2-| sort)
# 查找所有 Go 代码文件,排除测试文件
files=$(find . -name "*.go" -not -name "*_test.go" | sort)

declare -i e=0
declare -i m=0
declare -i h=0
# 初始化统计变量
declare -A counts=( ["E"]=0 ["M"]=0 ["H"]=0 )

# 遍历文件并统计各个难度的数量
for f in $files; do
if [[ "$f" =~ ^.*/E[0-9.]+_.*$ ]]; then
e+=1
fi

if [[ "$f" =~ ^.*/M[0-9.]+_.*$ ]]; then
m+=1
fi

if [[ "$f" =~ ^.*/H[0-9.]+_.*$ ]]; then
h+=1
if [[ "$f" =~ /E[0-9.]+_ ]]; then
((counts["E"]++))
elif [[ "$f" =~ /M[0-9.]+_ ]]; then
((counts["M"]++))
elif [[ "$f" =~ /H[0-9.]+_ ]]; then
((counts["H"]++))
fi
done

# 生成 README.md 文件
cat << EOF > README.md
#
| 难度 | 完成 |
| ---- | ----|
| 简单 | $e |
| 中等 | $m |
| 困难 | $h |
| 简单 | ${counts["E"]} |
| 中等 | ${counts["M"]} |
| 困难 | ${counts["H"]} |
EOF
15 changes: 15 additions & 0 deletions problems/math/E9_PalindromeNumber.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package math

func isPalindrome(x int) bool {
if x < 0 || (x%10 == 0 && x != 0) {
return false
}

rev := 0
for x > rev {
rev = rev*10 + x%10
x /= 10
}

return x == rev || x == rev/10
}
29 changes: 29 additions & 0 deletions problems/math/E9_PalindromeNumber_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package math

import "testing"

func TestIsPalindrome(t *testing.T) {
tests := []struct {
input int
expected bool
}{
{121, true}, // 正数回文
{-121, false}, // 负数不是回文
{10, false}, // 非回文
{12321, true}, // 长回文数
{1, true}, // 单一数字
{0, true}, // 0 是回文
{1221, true}, // 偶数位回文
{123, false}, // 非回文
{-2147447412, false}, // 负数不是回文
}

for _, tt := range tests {
t.Run("", func(t *testing.T) {
result := isPalindrome(tt.input)
if result != tt.expected {
t.Errorf("isPalindrome(%d) = %v; want %v", tt.input, result, tt.expected)
}
})
}
}
11 changes: 11 additions & 0 deletions problems/string/E168_ExcelSheetColumnTitle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package string

func convertToTitle(columnNumber int) string {
res := ""
for columnNumber > 0 {
columnNumber--
res = string(rune('A'+columnNumber%26)) + res
columnNumber = columnNumber / 26
}
return res
}
26 changes: 26 additions & 0 deletions problems/string/E168_ExcelSheetColumnTitle_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package string

import (
"testing"
)

func TestConvertToTitle(t *testing.T) {
tests := []struct {
columnNumber int
expected string
}{
{1, "A"},
{28, "AB"},
{701, "ZY"},
{2147483647, "FXSHRXW"},
}

for _, tt := range tests {
t.Run(tt.expected, func(t *testing.T) {
result := convertToTitle(tt.columnNumber)
if result != tt.expected {
t.Errorf("convertToTitle(%d) = %s; want %s", tt.columnNumber, result, tt.expected)
}
})
}
}
30 changes: 14 additions & 16 deletions problems/string/M3_LongestSubstrWithoutRepeatingChars.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
package string

func lengthOfLongestSubstring(s string) int {
max := func(x, y int) int {
if x > y {
return x
}
return y
}
mp := make(map[byte]int)
l, res, slow := len(s), 0, 0

mp := make(map[byte]struct{})
l, res, slow, fast := len(s), 0, 0, 0
for slow < l && fast < l {
for fast := 0; fast < l; fast++ {
cFast := s[fast]
if _, ok := mp[cFast]; !ok {
mp[cFast] = struct{}{}
fast++
res = max(res, fast-slow)
} else {
delete(mp, s[slow])
slow++
if i, ok := mp[cFast]; ok {
slow = max(slow, i+1)
}
mp[cFast] = fast
res = max(res, fast-slow+1)
}

return res
}

func max(x, y int) int {
if x > y {
return x
}
return y
}

func lengthOfLongestSubstring2(s string) int {
max := func(x, y int) int {
if x > y {
Expand Down
39 changes: 39 additions & 0 deletions problems/string/M3_LongestSubstrWithoutRepeatingChars_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,68 @@ func Test_lengthOfLongestSubstring(t *testing.T) {
want int
}{
{
name: "Test with abcabcbb",
args: args{
s: "abcabcbb",
},
want: 3,
},
{
name: "Test with bbbbbbbbb",
args: args{
s: "bbbbbbbbb",
},
want: 1,
},
{
name: "Test with pwwkew",
args: args{
s: "pwwkew",
},
want: 3,
},
{
name: "Test with empty string",
args: args{
s: "",
},
want: 0,
},
{
name: "Test with a",
args: args{
s: "a",
},
want: 1,
},
{
name: "Test with au",
args: args{
s: "au",
},
want: 2,
},
{
name: "Test with dvdf",
args: args{
s: "dvdf",
},
want: 3,
},
{
name: "Test with anviaj",
args: args{
s: "anviaj",
},
want: 5,
},
{
name: "Test with tmmzuxt",
args: args{
s: "tmmzuxt",
},
want: 5,
},
}
for _, tt := range tests {
for _, f := range []func(string) int{lengthOfLongestSubstring, lengthOfLongestSubstring2} {
Expand Down
82 changes: 82 additions & 0 deletions problems/string/M8_atoi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package string

import "math"

const (
INT_MAX = math.MaxInt32
INT_MIN = math.MinInt32
)

const (
START = iota
SIGNED
IN_NUMBER
END
)

type Automaton struct {
state int
sign int
ans int
table map[int][]int
}

func NewAutomaton() *Automaton {
return &Automaton{
state: START,
sign: 1,
ans: 0,
table: map[int][]int{
START: {START, SIGNED, IN_NUMBER, END},
SIGNED: {END, END, IN_NUMBER, END},
IN_NUMBER: {END, END, IN_NUMBER, END},
END: {END, END, END, END},
},
}
}

func (a *Automaton) getCol(c byte) int {
if c == ' ' {
return 0
}
if c == '+' || c == '-' {
return 1
}
if c >= '0' && c <= '9' {
return 2
}
return 3
}

func (a *Automaton) transition(c byte) {
a.state = a.table[a.state][a.getCol(c)]
if a.state == IN_NUMBER {
a.ans = a.ans*10 + int(c-'0')
if a.sign == 1 {
a.ans = min(a.ans, INT_MAX)
} else {
a.ans = min(a.ans, -INT_MIN)
}
} else if a.state == SIGNED {
if c == '+' {
a.sign = 1
} else {
a.sign = -1
}
}
}

func min(a, b int) int {
if a < b {
return a
}
return b
}

func myAtoi(s string) int {
automaton := NewAutomaton()
for i := 0; i < len(s); i++ {
automaton.transition(s[i])
}
return automaton.sign * automaton.ans
}
Loading

0 comments on commit 09dc742

Please sign in to comment.