当前位置:网站首页>[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;
}
};
边栏推荐
- Nifi from introduction to practice (nanny level tutorial) - flow
- 在ntpdate同步时间的时候出现“the NTP socket is in use, exiting”
- 探索Cassandra的去中心化分布式架构
- TCP拥塞控制详解 | 3. 设计空间
- 疫情常态化大背景下,关于远程办公的思考|社区征文
- Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (II)
- Remote file contains actual operation
- Client does not support authentication protocol requested by server; consider upgrading MySQL client
- ThreeJS 第二篇:顶点概念、几何体结构
- Pyinstaller is not an internal or external command, nor is it a runnable program or batch file
猜你喜欢
[solved] access denied for user 'root' @ 'localhost' (using password: yes)
跟我学企业级flutter项目:简化框架demo参考
Mixlab编辑团队招募队友啦~~
8个酷炫可视化图表,快速写出老板爱看的可视化分析报告
2022爱分析· 国央企数字化厂商全景报告
于文文、胡夏等明星带你玩转派对 皮皮APP点燃你的夏日
Uploads labs range (with source code analysis) (under update)
Zebras are recognized as dogs, and Stanford found the reason why AI made mistakes
[combinatorics] non descending path problem (outline of non descending path problem | basic model of non descending path problem | non descending path problem expansion model 1 non origin starting poi
Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (III)
随机推荐
LeetCode1491. Average value of wages after removing the minimum wage and the maximum wage
Thinking about telecommuting under the background of normalization of epidemic | community essay solicitation
Thread pool executes scheduled tasks
如何在本机搭建SVN服务器
Central South University | through exploration and understanding: find interpretable features with deep reinforcement learning
《天天数学》连载56:二月二十五日
Extraction of the same pointcut
Unity项目优化案例一
【剑指 Offer 】57 - II. 和为s的连续正数序列
Rk3399 platform development series explanation (WiFi) 5.54. What is WiFi wireless LAN
Hong Kong Polytechnic University | data efficient reinforcement learning and adaptive optimal perimeter control of network traffic dynamics
8 cool visual charts to quickly write the visual analysis report that the boss likes to see
[statement] about searching sogk1997 and finding many web crawler results
Learn from me about the enterprise flutter project: simplified framework demo reference
First knowledge of database
在ntpdate同步时间的时候出现“the NTP socket is in use, exiting”
远程办公之大家一同实现合作编辑资料和开发文档 | 社区征文
"Everyday Mathematics" serial 56: February 25
斑马识别成狗,AI犯错的原因被斯坦福找到了
Add color to the interface automation test framework and realize the enterprise wechat test report