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
// @Title: 仅仅反转字母 (Reverse Only Letters)
// @Author: 15816537946@163.com
// @Date: 2022-02-23 10:54:26
// @Runtime: 0 ms
// @Memory: 1.8 MB
func reverseOnlyLetters(s string) string {
	lo, hi :=  0, len(s)-1
    s1 := []byte(s)
	for lo < hi {
		for lo<len(s) && !isAlpha(s[lo]) {
			lo++
		}
		for hi >=0 && !isAlpha(s[hi]) {
			hi--
		}

        if lo >=hi {
            break
        }

		s1[lo], s1[hi] = s1[hi], s1[lo]
        lo++
        hi--
	}

	return string(s1)

}

func isAlpha(a byte) bool {
	if a >= 'a' && a <='z' {
		return true
	}
	if a >='A' && a <='Z' {
		return true
	}
	return false
}