Skip to content

Commit

Permalink
Create 9. Palindrome Number.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
hak2711 authored Mar 27, 2022
1 parent 3cc6690 commit 5e4b857
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions 박현아/leetcode/9. Palindrome Number.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//220327
//leetcode #9 Palindrome Number /Easy
//https://leetcode.com/problems/palindrome-number/


//1
#include <string>

class Solution {
public:
bool isPalindrome(int x) {
string x_s = to_string(x);
string x_reversed(x_s);

std::reverse(x_reversed.begin(), x_reversed.end());

return (x_s == x_reversed);
}
};


//2 faster
#include <string>

class Solution {
public:
bool isPalindrome(int x) {
string x_s = to_string(x);

if(x_s[0] == '-')
return false;

for(int i = 0; i<(x_s.size()/2); i++){
//be careful for the last index of x_s. It is not x_s.size() but x_s.size()-1
if(x_s[i] != x_s[x_s.size()-i-1])
return false;
}

return true;
}
};

0 comments on commit 5e4b857

Please sign in to comment.