-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpalindrome.cpp
More file actions
31 lines (28 loc) · 975 Bytes
/
Copy pathpalindrome.cpp
File metadata and controls
31 lines (28 loc) · 975 Bytes
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
31
/*
LeetCode: Problem 9: Palindrome Number
Difficulty Level: Easy
Name: Nava Nizard
Date: May 9, 2025
Programming Language: C++
Instructions: Given an integer x, return true if x is a palindrome, and false otherwise. (A palindrome
number is a number that reads the same from left to right as from right to left.)
Approach: Typecast the integer to a string, have two indices (pointers) from both ends of the string
and see if they're equal.
*/
class Solution {
public:
bool isPalindrome(int x) {
string num = to_string(x); //typecast to string
int len = num.length();
int i = 0;
int j = len - 1;
while(i < (len / 2)){ //until half the string
if (num[i] != num[j]){ //if you find a place where they're not equal
return false;
}
i++;
j--;
}
return true;
}
};