-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathac784.go
More file actions
48 lines (43 loc) · 986 Bytes
/
Copy pathac784.go
File metadata and controls
48 lines (43 loc) · 986 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package problem784
// 递归解法
// 每当遇到一个字符时,分别统计 1.该字符为小写时的所有情况 2.该字符为大写时的所有情况
func letterCasePermutation(S string) []string {
ans := make([]string, 0)
if len(S) < 1 {
return []string{""}
}
return core(ans, []byte(S), 0)
}
func core(ans []string, chars []byte, index int) []string {
if index == len(chars) {
ans = append(ans, string(chars))
} else {
if isLetter(chars[index]) {
chars[index] = toLower(chars[index])
ans = core(ans, chars, index+1)
chars[index] = toUpper(chars[index])
}
ans = core(ans, chars, index+1)
}
return ans
}
func isLetter(c byte) bool {
if (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') {
return true
}
return false
}
func toLower(c byte) byte {
if c >= 'A' && c <= 'Z' {
dis := byte('a' - 'A')
return c + dis
}
return c
}
func toUpper(c byte) byte {
if c >= 'a' && c <= 'z' {
dis := byte('a' - 'A')
return c - dis
}
return c
}