728x90
https://leetcode.com/problems/palindrome-number/submissions/
정수를 문자열로 바꾸고 앞 뒤 인덱스를 통해 문자열을 비교했다.
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
bool isPalindrome(int x){
string str = to_string(x);
int len = str.length();
for(int i=0; i<len/2; i++){
if(str[i] != str[len-1-i]) return false;
}
return true;
}
int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cout << isPalindrome(121) << '\n';
cout << isPalindrome(123) << '\n';
}
728x90
'알고리즘 > LeetCode' 카테고리의 다른 글
[LeetCode/easy] strStr() (0) | 2022.08.12 |
---|---|
[LeetCode/easy] Valid Parentheses (0) | 2022.08.12 |
[LeetCode/easy] Longest Common Prefix (0) | 2022.04.18 |
[LeetCode/easy] Roman To Integer (0) | 2022.04.18 |
[LeetCode/easy] TwoSum (0) | 2022.04.18 |