-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path9. Palindrome Number.cs
More file actions
19 lines (17 loc) · 1 KB
/
9. Palindrome Number.cs
File metadata and controls
19 lines (17 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class Solution {
public bool IsPalindrome(int x) {
String temp = "";
for (int i = 0; i < x.ToString().Length; i++) {
temp += x.ToString()[x.ToString().Length - i - 1];
}
return (x.ToString() == temp);
}
}
// The overall time complexity is O(n), where n is the number of characters in the string representing x.
//
// The process I went through here is that I knew I could convert the integer to a string, and compare itself with its
// reversed string, making the overall time complexity O(n). Here is the breakdown of my code:
// 1. I start by initializing the temp string which I will be used at the end of the code.
// 2. Then I open a for loop which will loop through all characters of the string version of x.
// 3. In the for loop, I add to the temp string all characters from the string version of x but in reverse.
// 4. Finally I return the boolean expression comparing the string version of x and its reversed string temp.