把一個字串轉為小寫後, 將字串內除了英文跟數字以外的字元全部移除...
leetcode

Valid Palindrome

把一個字串轉為小寫後, 將字串內除了英文跟數字以外的字元全部移除, 驗證該字串, 正向與反向的字元排序相同

解題脈絡

[第一版]先把字串轉小寫, 並且把非英文與數字排除後, 再來去比對文字順序

寫法

func isPalindrome(s string) bool {
	low_str := strings.ToLower(s)
	tempstr := ""
	tempstr2 := ""
	for _, c := range low_str {
		if (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') {
			tempstr = fmt.Sprintf("%v%v", tempstr, string(c))
		}
		continue
	}
	if len(tempstr) == 0 {
		return true
	}
	for i := len(tempstr) - 1; i >= 0; i-- {
		tempstr2 = fmt.Sprintf("%v%v", tempstr2, string(tempstr[i]))
	}
	if tempstr == tempstr2 {
		return true
	}
	return false
}

這版….跑非常久, 佔用記憶體還很多 QQ

只能說我對函數的使用還不夠熟練….還有想法太直覺了