Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solution to [Climbing Stairs] and [Reverse Integer] LeetCode Problems | Python #1034

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Python/climbing_stairs.py
Original file line number Diff line number Diff line change
@@ -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)

25 changes: 25 additions & 0 deletions Python/reverse_integer.py
Original file line number Diff line number Diff line change
@@ -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