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) + 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