Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 서버에도커설치
- 출처 코딩셰프
- 스프링부트사진올리기
- 스프링이미지업로드
- 스프링익셉션처리
- 스프링부트중복예외처리
- springboot_exception_handler
- centos도커설치
- 스프링부트구독취소
- ssh도커설치
- 출처 문어박사
- WAS웹서버
- vm도커설치하는법
- 스프링부트api
- 출처 따배도
- 출처 메타코딩
- 인스타클론
- 스프링부트팔로우취소
- 우분투도커설치
- dockerinstall
- 스프링사진
- 도커설치하는법
- 파이썬sort
- 스프링구독
- 멀티폼
- 스프링부트
- 스프링부트팔로잉
- 스프링부트서버에사진전송
- 출처 노마드코더
- 스프링사진업로드
Archives
- Today
- Total
MakerHyeon
[LeetCode] 125. Valid Palindrome (python,C++) 본문
https://leetcode.com/problems/valid-palindrome/description/
Valid Palindrome - LeetCode
Can you solve this real interview question? 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 cha
leetcode.com
- ord(문자)
하나의 문자를 인자로 받고 해당 문자에 해당하는 유니코드 정수를 반환
ex_ ord('a')를 넣으면 정수 97을 반환 - chr(정수)
하나의 정수를 인자로 받고 해당 정수에 해당하는 유니코드 문자를 반환
인자(정수)의 유효 범위는 0 ~ 1,114,111 (16진수 0x10 FFFF)까지 입니다.
ex_ chr(97)을 하면 문자 'a'를 반환
SOLUTION CODE
# PYTHON
1) 메모리복잡도 O(n)
class Solution:
def isPalindrome(self, s: str) -> bool:
newStr=""
for c in s:
if c.isalnum():
newStr+=c.lower()
return newStr==newStr[::-1]
1) 메모리복잡도 O(1)
class Solution:
def isPalindrome(self, s: str) -> bool:
l, r = 0, len(s)-1
while l < r:
while l < r and not self.alphaNum(s[l]):
l+=1
while r > l and not self.alphaNum(s[r]):
r-=1
if s[l].lower() != s[r].lower():
return False
l, r = l+1,r-1
return True
def alphaNum(self,c):
return (ord('A') <= ord(c) <= ord('Z') or
ord('a')<=ord(c)<=ord('z') or
ord('0') <= ord(c)<=ord('9'))
# C++
class Solution {
public:
bool isPalindrome(string s) {
int l=0;
int r=s.size()-1;
while(l<r){
while(l<r && !isalnum(s[l])) l++;
while(r>l && !isalnum(s[r])) r--;
if(tolower(s[l])!=tolower(s[r])) return false;
l++;
r--;
}
return true;
}
};
'Algorithm > LeetCode' 카테고리의 다른 글
[LeetCode] 167. Two Sum II - Input Array Is Sorted (python,C++) (0) | 2023.03.13 |
---|---|
[LeetCode] 707. Design Linked List (python,C++) (0) | 2023.03.13 |
[LeetCode] 128. Longest Consecutive Sequence (python,C++) (0) | 2023.03.08 |
[LeetCode] 238. Product of Array Except Self (0) | 2023.01.19 |
[LeetCode] 21. Merge Two Sorted Lists (0) | 2023.01.19 |
Comments