-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
702716b
commit 291b5be
Showing
3 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# | ||
# @lc app=leetcode id=70 lang=ruby | ||
# | ||
# [70] Climbing Stairs | ||
# | ||
# https://leetcode.com/problems/climbing-stairs/description/ | ||
# | ||
# You are climbing a stair case. It takes n steps to reach to the top. | ||
# | ||
# Each time you can either climb 1 or 2 steps. In how many distinct | ||
# ways can you climb to the top? | ||
# | ||
# Note: Given n will be a positive integer. | ||
# | ||
# Example 1: | ||
# | ||
# Input: 2 | ||
# Output: 2 | ||
# Explanation: There are two ways to climb to the top. | ||
# 1. 1 step + 1 step | ||
# 2. 2 steps | ||
# | ||
# Example 2: | ||
# | ||
# Input: 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 | ||
|
||
|
||
# @param {Integer} n | ||
# @return {Integer} | ||
def climb_stairs(n) | ||
Hash.new { |h, k| h[k] = h[k-1] + h[k-2] }.merge({1=>1, 2=>2})[n] | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# @param {Integer[]} nums1 | ||
# @param {Integer} m | ||
# @param {Integer[]} nums2 | ||
# @param {Integer} n | ||
# @return {Void} Do not return anything, modify nums1 in-place instead. | ||
def merge(nums1, m, nums2, n) | ||
i, j, k = n - 1, m - 1, nums1.size - 1 | ||
|
||
while i >= 0 | ||
if j < 0 | ||
nums1[k] = nums2[i] | ||
i -= 1 | ||
elsif nums1[j] > nums2[i] | ||
nums1[k] = nums1[j] | ||
j -= 1 | ||
else | ||
nums1[k] = nums2[i] | ||
i -= 1 | ||
end | ||
k -= 1 | ||
end | ||
|
||
nums1 | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
def delete_duplicates(head) | ||
cursor = head | ||
|
||
while cursor and cursor.next | ||
if cursor.next.val == cursor.val | ||
cursor.next = cursor.next.next | ||
else | ||
cursor = cursor.next | ||
end | ||
end | ||
|
||
head | ||
end |