-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
294 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,6 @@ | |
|
||
| 难度 | 完成 | | ||
| ---- | ----| | ||
| 简单 | 116 | | ||
| 中等 | 60 | | ||
| 简单 | 118 | | ||
| 中等 | 61 | | ||
| 困难 | 4 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.