Last active
January 26, 2021 05:52
-
-
Save mumunuu/60fa2cd6277b7c2b103250f3d5e8ffaa to your computer and use it in GitHub Desktop.
algorithm(leetcode) Valid Palindrome
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
func isPalindrome(s string) bool { | |
//문자와 숫자만 리턴해주는 함수를 정의 | |
remove := func(c rune) bool { | |
return !unicode.IsLetter(c) && !unicode.IsNumber(c) | |
} | |
//FieldsFunc를 이용해 문자와 숫자를 없애주고, Join으로 스트링으로 변환해주고, lowercase로 변환 | |
s = strings.ToLower(strings.Join(strings.FieldsFunc(s, remove), "")) | |
r := reverseString(s) //s 문자열을 반대로 돌려줌 | |
//절반만 돌아도 확인 가능 | |
for i := 0; i < len(s)/2; i++ { | |
if s[i] != r[i] { | |
return false | |
} | |
} | |
return true | |
} | |
func reverseString(s string) string { //string을 거꾸로 반환하는 함수 | |
runes := []rune(s) | |
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 { | |
runes[i], runes[j] = runes[j], runes[i] | |
} | |
return string(runes) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment