-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
48-xxubin04 #182
48-xxubin04 #182
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ํ์ด์ฌ์ ํฐ๋ฆฐ๋๋กฌ ํ๊ธฐ ์ฐธ ์ข์ ์ธ์ด๋ ๋ง์ด์ฃ ...
class Solution:
def isPalindrome(self, s: str) -> bool:
s = ''.join(c.lower() for c in s if c.isalnum())
return s == s[::-1]
์ฌ์ค, ํฌํฌ์ธํฐ๋ฅผ ์ฌ์ฉํ๋ฉด ํ ๋ฒ ์ํํ๋ ๊ฒ๋ง์ผ๋ก๋ ๋ฌธ์์ด ํํฐ๋ง + ํฐ๋ฆฐ๋๋กฌ ๊ฒ์ฌ๊ฐ ๊ฐ๋ฅํ๋ต๋๋ค :)
class Solution:
def isPalindrome(self, s: str) -> bool:
lo, hi = 0, len(s) - 1
while lo < hi:
if not s[lo].isalnum(): # ํํฐ๋ง
lo += 1
continue
if not s[hi].isalnum(): # ํํฐ๋ง
hi -= 1
continue
if s[lo].lower() != s[hi].lower(): # ํฐ๋ฆฐ๋๋กฌ ๊ฒ์ฌ
return False
lo += 1
hi -= 1
return True
์์๊ฐ ํฌํฌ์ธํฐ, ์๋๊ฐ ๋ค์ง๊ธฐ ๊ธฐ๋ฅ์ ์ฌ์ฉํ ํ์ด์
๋๋ค.
์ข ์ฐจ์ด๋์ฃ ? :)
ans = deque() | ||
s_list = list(s) | ||
for i in s_list: | ||
if i.isalnum(): | ||
ans.append(i.lower()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ฆฌ์คํธ ์ปดํ๋ฆฌํจ์ ๊ณผ ๋ฌธ์์ด ์กฐ์ธ ๊ธฐ๋ฅ์ ํ์ฉํ๋ค๋ฉด ๋ฌธ์์ด s์ ํํฐ๋ง์ด ๋งค์ฐ ๊ฐํธํด์ง๋๋ค :)
filtered_str = ''.join(c.lower() for c in s if c.isalnum())
if ''.join(ans) == ''.join(reversed(ans)): | ||
return True | ||
return False |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ํ์ด์ฌ์ ๋ฆฌ์คํธ ์ธ๋ฑ์ฑ ๊ธฐ๋ฅ์ ํ์ฉํ๋ฉด, ๊ฑฐ๊พธ๋ก ์ํํ๋ ๊ฑธ ์์ฃผ ์ฌํํ๊ฒ ์ํํ ์ ์์ต๋๋ค :)
reversed_str = filtered_str[::-1] # [์ฒ์๋ถํฐ:๋๊น์ง:์ญ์์ผ๋ก]
๊ฒฐ๊ตญ, ์ต์ข ์ ์ผ๋ก ํํฐ๋ง๋ ๋ฌธ์์ด๊ณผ ํํฐ๋ง๋ ๋ฌธ์์ด์ ๋ค์ง์ ๊ฒ "๋์ผํ๋ ์๋์ผํ๋"์ ๋ํ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํํด์ฃผ๋ ๊ฒ์ด๋, ๊ทธ ๋์ ๋น๊ตํ ๊ฒฐ๊ณผ ์์ฒด๋ฅผ ๋ฐํํด์ฃผ๋ฉด ๋๊ฒ ์ฃ ?
return filtered_str == reversed_str
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ ๋ ํด๋น s์ ๊ธธ์ด๊ฐ ๊ธธ์ง์์์, ๋ฐ๋ณต๋ฌธ์ผ๋ก ํด๊ฒฐํ์ต๋๋ค.
ํน์๋ฌธ์๋ฅผ ์ ์ธํ sentence๋ฅผ ๋ง๋ญ๋๋ค.
์ดํ์ sentence์ ๊ธธ์ด์ ๋ฐ๋งํผ ๋ฐ๋ณต ์ ๊ทผํ์ฌ ์ ๋ค ์ธ๋ฑ์ค์ ๋ฌธ์๋ผ๋ฆฌ ๋น๊ตํ์ต๋๋ค
ํฐ๋ฆฐ๋๋กฌ ๋๋ฌด ์ค๋๋ง์ ํ์ด์ ๋ฐ๊ฐ๋ค์ฌ ใ
๐โโ๏ธ
import java.util.*;
class Solution {
public boolean isPalindrome(String s) {
StringBuilder sb = new StringBuilder();
for(int i = 0; i < s.length(); i++) {
char now = s.charAt(i);
if(Character.isAlphabetic(now)|| Character.isDigit(now)) {
sb.append(now);
}
}
String sentence = sb.toString().toLowerCase();
int N = sentence.length();
for(int i = 0; i < N / 2; i++) {
char left = sentence.charAt(i);
char right = sentence.charAt(N-i-1);
if(left != right) return false;
}
return true;
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ ๋ filter์ฌ์ฉํด์ ๋ถํ์ํ ๋ถ๋ถ์ ์ ๊ฑฐํ๊ณ ,
๋ฌธ์์ด ๊ธธ์ด์ ์ ๋ฐ๋ง ํ์ํ๋ ๋ฐฉ๋ฒ์ผ๋ก ํ์์ต๋๋ค!
class Solution:
def isPalindrome(self, s: str) -> bool:
s = [i.lower() for i in filter(str.isalnum, s)]
n = len(s)
for i in range(n//2):
if s[i] != s[-i-1]:
return False
return True
๋ค์ํ ํ์ด๋ฒ์ ์ ์ ์์ด์ ์ฌ๋ฐ๋ค์ ๐๐
๐ ๋ฌธ์ ๋งํฌ
LeetCode 125. Valid Palindrome
โ๏ธ ์์๋ ์๊ฐ
30๋ถ
โจ ์๋ ์ฝ๋
1. ๋ฌธ์ ์ดํด
์ฃผ์ด์ง๋ ๋ฌธ์ฅ์์ ์์ด์ ์ซ์ ์ธ์ ์์๋ฅผ ์ ์ธํ ๋จ์ด๊ฐ ํฐ๋ฆฐ๋๋กฌ์ ์ฑ๋ฆฝํ๋ฉด true, ์ฑ๋ฆฝํ์ง ์์ผ๋ฉด false๋ฅผ ๋ฐํํ๋ค.
2. ์ฝ๋ ๋ถ์
๊ต์ฅํ ๊ฐ๋จํ๊ฒ ํ๋ฆฐ๋ค. ์ฃผ์ด์ง๋ ๋ฌธ์์ด
s
๋ฅผ ๋ฆฌ์คํธํํ์ฌ ํ ๊ธ์์ฉ ๋ถ๋ฆฌํs_list
๋ฅผ for๋ฌธ์ ํตํด ์ํํ๋ค.์ด ๊ณผ์ ์์
.alnum()
์กฐ๊ฑด์ผ๋ก, ์ํ๋ฒณ์ด๊ฑฐ๋ ์ซ์์ธ ๊ฒฝ์ฐ์๋ans
์ ์๋ฌธ์๋ก ๋ค์ ๋ถ์ธ๋ค.์ํ๋ฒณ๊ณผ ์ซ์๋ง ์ฃผ๋ฃจ๋ฃฉ ๋ถ์ด์๋ ๋ฌธ์์ธ
ans
์ ์ญ์์ผ๋ก ๋ถ์ธans
๊ฐ ๊ฐ์ ๊ฒฝ์ฐ์๋ True๋ฅผ ๋ฐํํ๊ณ ๊ทธ ์ธ์๋ False๋ฅผ ๋ฐํํ๋ค.์ด๋ ์กฐ๊ธ ํค๋งธ๋ ๊ฒ, ๋ฌธ์ ์์๋ true, false๋ฅผ ๋ฐํํ๋ค๊ณ ํด์ ๋ฌธ์์ด๋ก "true", "false"๋ฅผ ๋ฐํํ๋๋ฐ ์๋ฌด๋ฆฌ ๊ณ ์ณ๋ ํ ์ผ๊ฐ ํต๊ณผ๋์ง ์๋๋ผ....
๋ฌธ์ ์์ ์๋ฌธ์๋ก ๋ฐํํด๋ ์ค๋์๊ฒ boolean ํ์์ธ True, False๋ฅผ ๋ฐํํ๋ฉด ํต๊ณผํ๋ค.๐๐ป
์ฐธ๊ณ ์ฝ๋
์ฒ์ ์ง ์ฝ๋์ฌ์ ๋ง์ด ์ง์ ๋ถํ์ง๋ง ์ฌ๋ฆฝ๋๋ค. ใ ใ
์์ ์ฝ๋๊ฐ ์ผ์ชฝ, ์ฝ๋ ๋ถ์ํ ์ฝ๋๊ฐ ์ค๋ฅธ์ชฝ์ธ๋ฐ ์ฐจ์ด ๋ง์ด ๋๋ค์..๐ค
๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ
์์ต๋๋ค~