golang學習day3

作者: SecondRun (雨夜琴聲)   2024-01-25 10:45:30
125. Valid Palindrome
A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers.
Given a string s, return true if it is a palindrome, or false otherwise.
簡單說就是檢查是不是迴文
以下golang code
func isPalindrome(s string) bool {
slice := []int{}
for _, c := range s {
if c >= 97 && c <= 122 {
slice = append(slice, int(c))
continue
}
if c >= 65 && c <= 90 {
slice = append(slice, int(c+32))
}
if c >= 48 && c <= 57 {
slice = append(slice, int(c))
continue
}
}
i := 0
j := len(slice) - 1
for i < j {
if slice[i] == slice[j] {
i += 1
j -= 1
continue
}
return false
}
return true
}
不得不說golang在字串處理方面好像有點麻煩
資料結構也有點沒那麼強大的感覺
作者: sustainer123 (caster)   2023-01-25 10:45:00
大師
作者: SiranuiFlare (阿火)   2024-01-25 10:46:00
大師
作者: a000000000 (九個零喔)   2024-01-25 10:47:00
太師
作者: Firstshadow (IamCatづミ'_'ミづ)   2024-01-25 10:48:00
哪時要內推窩

Links booklink

Contact Us: admin [ a t ] ucptt.com