Leetcode Problems – Climbing Stairs

https://leetcode.com/problems/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?

 

Example 1:

Input: n = 2
Output: 2
Explanation: There are two ways to climb to the top.
1. 1 step + 1 step
2. 2 steps

Example 2:

Input: n = 3
Output: 3
Explanation: There are three ways to climb to the top.
1. 1 step + 1 step + 1 step
2. 1 step + 2 steps
3. 2 steps + 1 step

 

Constraints:

  • 1 <= n <= 45
				
					function climbStairs(nums: number) {
  let one = 1;
  let two = 1;
  for (let i = nums - 1; i > 0; i--) {
    let temp = one;
    [one, two] = [one + two, temp];
  }
  return one;
}
				
			

https://leetcode.com/problems/palindrome-number/

Given an integer x, return true if x is palindrome integer.

An integer is a palindrome when it reads the same backward as forward.

  • For example, 121 is a palindrome while 123 is not.

Example 1:

Input: x = 121
Output: true
Explanation: 121 reads as 121 from left to right and from right to left.

Example 2:

Input: x = -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.

Example 3:

Input: x = 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.

Constraints:

  • -231 <= x <= 231 - 1
				
					function isPalindrome(x: number) {
  if (x < 0) return false;
  let y = x.toString();
  for (let i = 0; i < y.length / 2; i++) {
    if (y[i] !== y[y.length - 1 - i]) return false;
  }
  return true;
}
				
			

Submission Detail

11510 / 11510 test cases passed.
Status: 

Accepted

Runtime: 215 ms
Memory Usage: 51.9 MB
Submitted: 3 months ago

Leave a Comment