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

# 面试题 64

## [To Index](https://github.com/newdee/Leetcode/blob/master/index.md)

## 面试题64. 求1+2+...+n

难度:Medium

> 求 1+2+...+n ，要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句（A?B:C）。

示例 1：

```
输入: n = 3 输出: 6 示例 2：

输入: n = 9 输出: 45
```

限制：

```
1 <= n <= 10000
```

简单递归：

```
class Solution {
vector<int> sums={1,3,6};
public:
    int sumNums(int n) {
        if(n<=sums.size()) return sums[n-1];
        return sumNums(n-1)+n;
    }
};
```

> 执行用时 :4 ms, 在所有 C++ 提交中击败了66.85%的用户\
> 内存消耗 :8.5 MB, 在所有 C++ 提交中击败了100.00%的用户
