> For the complete documentation index, see [llms.txt](https://jenhsuan.gitbook.io/algorithm/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jenhsuan.gitbook.io/algorithm/leetcode/70.-climbing-stairs.md).

# 70. Climbing Stairs

## 1.問題

* 一次只可以向前1或格, 有多少種方法到達終點?

![](/files/-LNAPFGJocMD39Jxo4kw)

## 2.想法

* 提問:
* function header, parameter
* test input
* 說明想法&#x20;
  * 動態規劃: 由步伐的種類有一步跟兩步, 可以知道到目前所在的位置可以由前一個位置或是前前一個位置而來, 因此到目前位置的所有可能方式為到前個位置的可能 + 到前前個位置的可能
* 測試計算複雜度

## 3.程式碼

```
class Solution {
public:
    int climbStairs(int n) {
        if ( n == 0) {
            return 0;
        }
        vector<int> cache(n, -1);
        return climb(n - 1, cache);
    }
private:
    int climb(int i, vector<int>& cache) {
        if (i == 0) {
            return 1;
        } else if (i == 1) {
            return 2;
        } 
        
        if (cache[i] == -1){
            cache[i] = climb(i - 1, cache) + climb(i - 2, cache);  
        }
        return cache[i];
    }
};
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://jenhsuan.gitbook.io/algorithm/leetcode/70.-climbing-stairs.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
