From 8bbe4ebfe4e0370d86f1af911da73c340096d38f Mon Sep 17 00:00:00 2001 From: Rudransh Joshi Date: Wed, 6 Oct 2021 00:16:51 +0530 Subject: [PATCH 1/2] Solution to Climbing Stairs (LeetCode) --- Python/climbing_stairs.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Python/climbing_stairs.py diff --git a/Python/climbing_stairs.py b/Python/climbing_stairs.py new file mode 100644 index 0000000..4b32e72 --- /dev/null +++ b/Python/climbing_stairs.py @@ -0,0 +1,21 @@ +""" +Link to Problem: https://leetcode.com/problems/climbing-stairs/ + +70. Climbing Stairs +You are climbing a staircase. It takes n steps to reach the top. +Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? +""" + +class Solution: + def fibonacci(self, num): + if num <= 1: + return num + return self.fibonacci(num-1) + self.fibonacci(num-2) + + def climbStairs(self, n): + """ + :type n: int + :rtype: int + """ + return self.fibonacci(n+1) + From 9549ae94503e51bd78642981cdf32bfaa420f413 Mon Sep 17 00:00:00 2001 From: Rudransh Joshi Date: Wed, 6 Oct 2021 00:44:00 +0530 Subject: [PATCH 2/2] Solution to Reverse Integer LeetCode Problem --- Python/reverse_integer.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Python/reverse_integer.py diff --git a/Python/reverse_integer.py b/Python/reverse_integer.py new file mode 100644 index 0000000..cd8abf7 --- /dev/null +++ b/Python/reverse_integer.py @@ -0,0 +1,25 @@ +""" +Link to Problem: https://leetcode.com/problems/reverse-integer/ + +7. Reverse Integer +Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0. +Assume the environment does not allow you to store 64-bit integers (signed or unsigned). +""" + +class Solution(object): + def reverse(self, x): + """ + :type x: int + :rtype: int + """ + if x == 0: + return 0 + + sign = None if not str(x).startswith("-") else "-" + x = str(abs(x))[::-1] + x = x.lstrip("0") + final = int(sign + x if sign else x) + + if (final <= (-2**31)) or (final >= (2**31 -1)): + return 0 + return final