Skip to content

Commit d19335f

Browse files
committed
面试题64. 求1+2+…+n
1 parent 891c214 commit d19335f

File tree

1 file changed

+16
-0
lines changed
  • leetcode-cn/面试题64. 求1+2+…+n

1 file changed

+16
-0
lines changed

leetcode-cn/面试题64. 求1+2+…+n/README.md

+16
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,20 @@ public:
4242
+ d(n, 7) + d(n, 8) + d(n, 9) + d(n, 10) + d(n, 11) + d(n, 12) + d(n, 13) + n) >> 1;
4343
}
4444
}
45+
```
46+
47+
## Solution 2.
48+
49+
```cpp
50+
// OJ: https://leetcode-cn.com/problems/qiu-12n-lcof/
51+
// Author: github.com/lzl124631x
52+
// Time: O(N)
53+
// Space: O(N)
54+
class Solution {
55+
public:
56+
int sumNums(int n) {
57+
n > 0 && (n += sumNums(n - 1));
58+
return n;
59+
}
60+
};
4561
```

0 commit comments

Comments
 (0)