当前位置:网站首页>[Jianzhi offer] 64 Find 1+2+... +n
[Jianzhi offer] 64 Find 1+2+... +n
2022-07-03 16:32:00 【LuZhouShiLi】
The finger of the sword Offer 64. seek 1+2+…+n
subject
seek 1+2+…+n , It is required that multiplication and division shall not be used 、for、while、if、else、switch、case Wait for keywords and conditional statements (A?B:C).
Ideas
- take if The judgment statement is converted into the method of short-circuit evaluation .
- Short circuit evaluation :if(A & &B) // If A yes True, Then keep watching B,A yes False, There is no need to see B;if(A || B) // If A yes True, Then there's no need to see B, If A yes Flase, Still have to see B.
- First write the ordinary recursive form , In the form of short-circuit evaluation
Code
- Short circuit evaluation
class Solution {
public:
int sumNums(int n) {
bool x = n > 1 && (n += sumNums(n - 1)) > 0;
return n;
}
};
- Recursive form
class Solution {
public:
int sumNums(int n) {
if(n == 1) return 1;
n += sumNums(n - 1);
return n;
}
};
边栏推荐
猜你喜欢

(补)双指针专题

Mixlab编辑团队招募队友啦~~

Initial test of scikit learn Library

Basis of target detection (IOU)

2022 love analysis · panoramic report of digital manufacturers of state-owned enterprises

什么是质押池,如何进行质押呢?

跟我学企业级flutter项目:简化框架demo参考

近视:摘镜or配镜?这些问题必须先了解清楚

Learn from me about the enterprise flutter project: simplified framework demo reference

Add color to the interface automation test framework and realize the enterprise wechat test report
随机推荐
arduino-esp32:LVGL项目(一)整体框架
Svn usage specification
Top k questions of interview
2022 love analysis · panoramic report of digital manufacturers of state-owned enterprises
Explore Netease's large-scale automated testing solutions see here see here
One article takes you to understand machine learning
Pychart error updating package list: connect timed out
Colab works with Google cloud disk
Remote file contains actual operation
A survey of state of the art on visual slam
[solved] access denied for user 'root' @ 'localhost' (using password: yes)
消息队列消息丢失和消息重复发送的处理策略
线程池执行定时任务
[combinatorics] combinatorial identity (sum of variable upper terms 1 combinatorial identity | summary of three combinatorial identity proof methods | proof of sum of variable upper terms 1 combinator
Is it safe to open an account with flush?
初试scikit-learn库
Netease UI automation test exploration: airtest+poco
Golang decorator mode and its use in NSQ
斑馬識別成狗,AI犯錯的原因被斯坦福找到了
用通达信炒股开户安全吗?