upd
This commit is contained in:
16
9. Palindrome Number/Solution.java
Normal file
16
9. Palindrome Number/Solution.java
Normal 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
BIN
9. Palindrome Number/a.out
Executable file
Binary file not shown.
26
9. Palindrome Number/solution.c
Normal file
26
9. Palindrome Number/solution.c
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
27
9. Palindrome Number/solution.cpp
Normal file
27
9. Palindrome Number/solution.cpp
Normal 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;
|
||||||
|
}
|
||||||
13
9. Palindrome Number/solution.py
Normal file
13
9. Palindrome Number/solution.py
Normal 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()
|
||||||
@@ -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) |
|
||||||
|
|
||||||
|
|||||||
BIN
Two Sum/a.out
BIN
Two Sum/a.out
Binary file not shown.
Reference in New Issue
Block a user