diff --git a/Two Sum/Solution.java b/1. Two Sum/Solution.java similarity index 100% rename from Two Sum/Solution.java rename to 1. Two Sum/Solution.java diff --git a/Two Sum/solution.c b/1. Two Sum/solution.c similarity index 100% rename from Two Sum/solution.c rename to 1. Two Sum/solution.c diff --git a/Two Sum/solution.cpp b/1. Two Sum/solution.cpp similarity index 100% rename from Two Sum/solution.cpp rename to 1. Two Sum/solution.cpp diff --git a/Two Sum/solution.py b/1. Two Sum/solution.py similarity index 100% rename from Two Sum/solution.py rename to 1. Two Sum/solution.py diff --git a/9. Palindrome Number/Solution.java b/9. Palindrome Number/Solution.java new file mode 100644 index 0000000..55fb598 --- /dev/null +++ b/9. Palindrome Number/Solution.java @@ -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); + } +} diff --git a/9. Palindrome Number/a.out b/9. Palindrome Number/a.out new file mode 100755 index 0000000..b48286e Binary files /dev/null and b/9. Palindrome Number/a.out differ diff --git a/9. Palindrome Number/solution.c b/9. Palindrome Number/solution.c new file mode 100644 index 0000000..d3a6797 --- /dev/null +++ b/9. Palindrome Number/solution.c @@ -0,0 +1,26 @@ +#include +#include + +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; +} + diff --git a/9. Palindrome Number/solution.cpp b/9. Palindrome Number/solution.cpp new file mode 100644 index 0000000..5b9c178 --- /dev/null +++ b/9. Palindrome Number/solution.cpp @@ -0,0 +1,27 @@ +#include +#include +#include + + +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; +} diff --git a/9. Palindrome Number/solution.py b/9. Palindrome Number/solution.py new file mode 100644 index 0000000..faf1ba6 --- /dev/null +++ b/9. Palindrome Number/solution.py @@ -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() diff --git a/README.md b/README.md index e69de29..a6685fe 100644 --- a/README.md +++ b/README.md @@ -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) | + diff --git a/Two Sum/a.out b/Two Sum/a.out deleted file mode 100755 index a5943ac..0000000 Binary files a/Two Sum/a.out and /dev/null differ