Skip to content

Commit 2d62393

Browse files
committed
70. Climbing Stairs
1 parent 94e56b5 commit 2d62393

File tree

3 files changed

+67
-29
lines changed

3 files changed

+67
-29
lines changed

README.md

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,41 @@
11
# LeetCode
22

3-
![Problems Solved](https://img.shields.io/badge/Problems%20Solved-28%20%2F%203008-1f425f?logo=leetcode)
3+
![Problems Solved](https://img.shields.io/badge/Problems%20Solved-29%20%2F%203009-1f425f?logo=leetcode)
44
![Language: Golang](https://img.shields.io/badge/language-Golang-00ADD8?logo=go)
55
![Language: Rust](https://img.shields.io/badge/language-Rust-00ADD8?logo=rust)
66
![Language: Bash](https://img.shields.io/badge/language-Bash-00ADD8?logo=gnubash&logoColor=f5f5f5)
77

88
### LeetCode Algorithm
99

10-
| # | Title | Solution | Difficulty | Topics |
11-
|----:|---------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------|:----------:|:------------------------------------:|
12-
| 1 | [Two Sum](https://leetcode.com/problems/two-sum/) | [Go](go/0001_two_sum) | 🟢 | Array, Hash Table |
13-
| 2 | [Add Two Numbers](https://leetcode.com/problems/add-two-numbers/) | [Go](go/0002_add_two_numbers) | 🟡 | Linked List, Math, Recursion |
14-
| 3 | [Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) | [Go](go/0003_longest_substring) | 🟡 | Hash Table, String, Sliding Window |
15-
| 7 | [Reverse Integer](https://leetcode.com/problems/reverse-integer/) | [Go](go/0007_reverse_integer) | 🟡 | Math |
16-
| 9 | [Palindrome Number](https://leetcode.com/problems/palindrome-number/) | [Go](go/0009_palindrome_number) | 🟢 | Math |
17-
| 13 | [Roman to Integer](https://leetcode.com/problems/roman-to-integer/) | [Go](go/0013_roman_to_integer) | 🟢 | Hash Table, Math, String |
18-
| 17 | [Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/) | [Go](go/0017_letter_combinations_phone) | 🟡 | Hash Table, String, Backtracking |
19-
| 20 | [Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) | [Go](go/0020_valid_parentheses) | 🟢 | String, Stack |
20-
| 26 | [Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/) | [Go](go/0026_remove_duplicates_from_array) | 🟢 | Array, Pointer |
21-
| 35 | [Search Insert Position](https://leetcode.com/problems/search-insert-position/) | [Go](go/0035_search_insert_position), [Rust](rust/_0035_search_insert_position) | 🟢 | Array, Binary Search |
22-
| 50 | [Pow(x, n)](https://leetcode.com/problems/powx-n/) | [Go](go/0050_powx_n) | 🟡 | Math, Recursion |
23-
| 69 | [Sqrt(x)](https://leetcode.com/problems/sqrtx/) | [Go](go/0069_sqrtx), [Rust](rust/_0069_sqrtx) | 🟢 | Math, Binary Search |
24-
| 75 | [Sort Colors](https://leetcode.com/problems/sort-colors/) | [Go](go/0075_sort_colors) | 🟡 | Array, Pointer, Sorting |
25-
| 88 | [Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array/) | [Go](go/0088_merge_sorted_array) | 🟢 | Array, Pointer |
26-
| 125 | [Valid Palindrome](https://leetcode.com/problems/valid-palindrome/) | [Go](go/0125_valid_palindrome) | 🟢 | String, Pointer |
27-
| 136 | [Single Number](https://leetcode.com/problems/single-number/) | [Go](go/0136_single_number) | 🟢 | Array, Bit Manipulation |
28-
| 155 | [Min Stack](https://leetcode.com/problems/min-stack/) | [Go](go/0155_min_stack) | 🟢 | Stack |
29-
| 217 | [Contains Duplicate](https://leetcode.com/problems/contains-duplicate/) | [Go](go/0217_contains_duplicate) | 🟢 | Array, Hash Table |
30-
| 226 | [Invert Binary Tree](https://leetcode.com/problems/invert-binary-tree/) | [Go](go/0226_invert_binary_tree) | 🟢 | Tree, BFS, DFS, Binary Tree |
31-
| 268 | [Missing Number](https://leetcode.com/problems/missing-number/) | [Go](go/0268_missing_number) | 🟢 | Array, Math, Bit Manipulation |
32-
| 283 | [Move Zeroes](https://leetcode.com/problems/move-zeroes/) | [Go](go/0283_move_zeroes) | 🟢 | Array, Pointer |
33-
| 344 | [Reverse String](https://leetcode.com/problems/reverse-string/) | [Go](go/0344_reverse_string), [Rust](rust/_0344_reverse_string) | 🟢 | String, Pointer |
34-
| 380 | [Insert Delete GetRandom O(1)](https://leetcode.com/problems/insert-delete-getrandom-o1/) | [Go](go/0380_insert_delete_getrandom_o1) | 🟡 | Array, Hash Table, Math, Randomized |
35-
| 404 | [Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves/) | [Go](go/0404_sum_of_left_leaves) | 🟢 | Tree, BFS, DFS, Binary Tree |
36-
| 540 | [Single Element in a Sorted Array](https://leetcode.com/problems/single-element-in-a-sorted-array/) | [Rust](rust/_0540_single_elem_in_array) | 🟡 | Array, Binary Search |
37-
| 704 | [Binary Search](https://leetcode.com/problems/binary-search/) | [Go](go/0704_binary_search), [Rust](rust/_0704_binary_search) | 🟢 | Array, Binary Search |
10+
| # | Title | Solution | Difficulty | Topics |
11+
|----:|---------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------|:----------:|:--------------------------------------:|
12+
| 1 | [Two Sum](https://leetcode.com/problems/two-sum/) | [Go](go/0001_two_sum) | 🟢 | Array, Hash Table |
13+
| 2 | [Add Two Numbers](https://leetcode.com/problems/add-two-numbers/) | [Go](go/0002_add_two_numbers) | 🟡 | Linked List, Math, Recursion |
14+
| 3 | [Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) | [Go](go/0003_longest_substring) | 🟡 | Hash Table, String, Sliding Window |
15+
| 7 | [Reverse Integer](https://leetcode.com/problems/reverse-integer/) | [Go](go/0007_reverse_integer) | 🟡 | Math |
16+
| 9 | [Palindrome Number](https://leetcode.com/problems/palindrome-number/) | [Go](go/0009_palindrome_number) | 🟢 | Math |
17+
| 13 | [Roman to Integer](https://leetcode.com/problems/roman-to-integer/) | [Go](go/0013_roman_to_integer) | 🟢 | Hash Table, Math, String |
18+
| 17 | [Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/) | [Go](go/0017_letter_combinations_phone) | 🟡 | Hash Table, String, Backtracking |
19+
| 20 | [Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) | [Go](go/0020_valid_parentheses) | 🟢 | String, Stack |
20+
| 26 | [Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/) | [Go](go/0026_remove_duplicates_from_array) | 🟢 | Array, Pointer |
21+
| 35 | [Search Insert Position](https://leetcode.com/problems/search-insert-position/) | [Go](go/0035_search_insert_position), [Rust](rust/_0035_search_insert_position) | 🟢 | Array, Binary Search |
22+
| 50 | [Pow(x, n)](https://leetcode.com/problems/powx-n/) | [Go](go/0050_powx_n) | 🟡 | Math, Recursion |
23+
| 69 | [Sqrt(x)](https://leetcode.com/problems/sqrtx/) | [Go](go/0069_sqrtx), [Rust](rust/_0069_sqrtx) | 🟢 | Math, Binary Search |
24+
| 70 | [Climbing Stairs](https://leetcode.com/problems/climbing-stairs/) | [Go](go/0070_climbing_stairs) | 🟢 | Math, Dynamic Programming, Memoization |
25+
| 75 | [Sort Colors](https://leetcode.com/problems/sort-colors/) | [Go](go/0075_sort_colors) | 🟡 | Array, Pointer, Sorting |
26+
| 88 | [Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array/) | [Go](go/0088_merge_sorted_array) | 🟢 | Array, Pointer |
27+
| 125 | [Valid Palindrome](https://leetcode.com/problems/valid-palindrome/) | [Go](go/0125_valid_palindrome) | 🟢 | String, Pointer |
28+
| 136 | [Single Number](https://leetcode.com/problems/single-number/) | [Go](go/0136_single_number) | 🟢 | Array, Bit Manipulation |
29+
| 155 | [Min Stack](https://leetcode.com/problems/min-stack/) | [Go](go/0155_min_stack) | 🟢 | Stack |
30+
| 217 | [Contains Duplicate](https://leetcode.com/problems/contains-duplicate/) | [Go](go/0217_contains_duplicate) | 🟢 | Array, Hash Table |
31+
| 226 | [Invert Binary Tree](https://leetcode.com/problems/invert-binary-tree/) | [Go](go/0226_invert_binary_tree) | 🟢 | Tree, BFS, DFS, Binary Tree |
32+
| 268 | [Missing Number](https://leetcode.com/problems/missing-number/) | [Go](go/0268_missing_number) | 🟢 | Array, Math, Bit Manipulation |
33+
| 283 | [Move Zeroes](https://leetcode.com/problems/move-zeroes/) | [Go](go/0283_move_zeroes) | 🟢 | Array, Pointer |
34+
| 344 | [Reverse String](https://leetcode.com/problems/reverse-string/) | [Go](go/0344_reverse_string), [Rust](rust/_0344_reverse_string) | 🟢 | String, Pointer |
35+
| 380 | [Insert Delete GetRandom O(1)](https://leetcode.com/problems/insert-delete-getrandom-o1/) | [Go](go/0380_insert_delete_getrandom_o1) | 🟡 | Array, Hash Table, Math, Randomized |
36+
| 404 | [Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves/) | [Go](go/0404_sum_of_left_leaves) | 🟢 | Tree, BFS, DFS, Binary Tree |
37+
| 540 | [Single Element in a Sorted Array](https://leetcode.com/problems/single-element-in-a-sorted-array/) | [Rust](rust/_0540_single_elem_in_array) | 🟡 | Array, Binary Search |
38+
| 704 | [Binary Search](https://leetcode.com/problems/binary-search/) | [Go](go/0704_binary_search), [Rust](rust/_0704_binary_search) | 🟢 | Array, Binary Search |
3839

3940
### LeetCode Shell
4041

go/0070_climbing_stairs/solution.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package climbing_stairs
2+
3+
func climbStairs(n int) int {
4+
if n <= 2 {
5+
return n
6+
}
7+
8+
prevSteps, currSteps := 1, 2 // 1 stair ways, 2 stairs ways
9+
for i := 3; i <= n; i++ {
10+
currSteps, prevSteps = prevSteps+currSteps, currSteps
11+
}
12+
13+
return currSteps
14+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package climbing_stairs
2+
3+
import "testing"
4+
5+
func Test_singleNumber(t *testing.T) {
6+
tests := []struct {
7+
input int
8+
want int
9+
}{
10+
{input: 1, want: 1},
11+
{input: 2, want: 2},
12+
{input: 3, want: 3},
13+
{input: 5, want: 8},
14+
{input: 45, want: 1836311903},
15+
}
16+
17+
for i, tc := range tests {
18+
got := climbStairs(tc.input)
19+
if tc.want != got {
20+
t.Fatalf("Case #%d: expected: %v, got: %v", i+1, tc.want, got)
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)