This commit is contained in:
2026-01-28 15:56:45 +03:00
parent 8bdba1f2f2
commit 3907f75973
11 changed files with 89 additions and 0 deletions

View File

@@ -0,0 +1,16 @@
class Solution {
public static void main(String[] args) {
int test[] = {121, -121, 10};
for (int t : test) {
System.out.println("isPalindrome(" + t + ") = " + isPalindrome(t));
}
}
public static boolean isPalindrome(int x) {
StringBuilder sb = new StringBuilder(Integer.toString(x));
sb.reverse();
String reversed = sb.toString();
return Integer.toString(x).equals(reversed);
}
}

BIN
9. Palindrome Number/a.out Executable file

Binary file not shown.

View File

@@ -0,0 +1,26 @@
#include <stdlib.h>
#include <stdbool.h>
bool isPalindrome(int x) {
// negatives and numbers ending in 0 (except 0 itself) are not palindromes
if (x < 0 || (x % 10 == 0 && x != 0))
return false;
int reversedHalf = 0;
while (x > reversedHalf) {
reversedHalf = reversedHalf * 10 + x % 10;
x /= 10;
}
// for even digits: x == reversedHalf
// for odd digits: x == reversedHalf / 10
return (x == reversedHalf) || (x == reversedHalf / 10);
}
int main(int argc, char *argv[])
{
return EXIT_SUCCESS;
}

View File

@@ -0,0 +1,27 @@
#include <algorithm>
#include <iostream>
#include <string>
class Solution {
public:
bool isPalindrome(int x) {
std::string s = std::to_string(x);
std::string reversed_s = s;
std::reverse(reversed_s.begin(), reversed_s.end());
return s == reversed_s;
}
};
int main (int argc, char *argv[]) {
Solution s;
int test[] = {121, -121, 10};
for (int t : test) {
std::cout << "isPalindrome(" << t << ") = " << s.isPalindrome(t) << std::endl;
}
return 0;
}

View File

@@ -0,0 +1,13 @@
class Solution:
def isPalindrome(self, x: int) -> bool:
return str(x) == str(x)[::-1]
def main() -> None:
test = [121, -121, 10] # True, False, False
s = Solution()
for t in test:
print(f"isPalindrome({t}) = {s.isPalindrome(t)}")
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,7 @@
here i've got some leetcode problems & solutions to them
| **Number** | **Name** | **Difficulty** | **Solution** |
| ---------- | -------- | -------------- | ------------ |
| 1 | *Two Sum* | Easy | [text](url) |
| 9 | *Palindrome Number* | Easy | [text](url) |

Binary file not shown.