当前位置:网站首页>[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;
}
};
边栏推荐
- 远程办公之大家一同实现合作编辑资料和开发文档 | 社区征文
- LeetCode1491. Average value of wages after removing the minimum wage and the maximum wage
- How to initialize views when loading through storyboards- How is view initialized when loaded via a storyboard?
- TCP congestion control details | 3 design space
- 【剑指 Offer】58 - II. 左旋转字符串
- Is it safe to open an account with flush?
- QT serial port UI design and solution to display Chinese garbled code
- Register in PHP_ Globals parameter settings
- Visual SLAM algorithms: a survey from 2010 to 2016
- Expression of request header in different countries and languages
猜你喜欢
Mb10m-asemi rectifier bridge mb10m
初试scikit-learn库
8 cool visual charts to quickly write the visual analysis report that the boss likes to see
Colab works with Google cloud disk
Mixlab编辑团队招募队友啦~~
消息队列消息丢失和消息重复发送的处理策略
8个酷炫可视化图表,快速写出老板爱看的可视化分析报告
MySQL single table field duplicate data takes the latest SQL statement
[solved] access denied for user 'root' @ 'localhost' (using password: yes)
关于视觉SLAM的最先进技术的调查-A survey of state-of-the-art on visual SLAM
随机推荐
远程办公之大家一同实现合作编辑资料和开发文档 | 社区征文
Client does not support authentication protocol requested by server; consider upgrading MySQL client
Unity project optimization case 1
中南大学|通过探索理解: 发现具有深度强化学习的可解释特征
近视:摘镜or配镜?这些问题必须先了解清楚
Pychart error updating package list: connect timed out
香港理工大学|数据高效的强化学习和网络流量动态的自适应最优周界控制
Visual SLAM algorithms: a survey from 2010 to 2016
Mixlab编辑团队招募队友啦~~
Yu Wenwen, Hu Xia and other stars take you to play with the party. Pipi app ignites your summer
[combinatorics] summary of combinatorial identities (eleven combinatorial identities | proof methods of combinatorial identities | summation methods)*
Unreal_DataTable 实现Id自增与设置RowName
(Supplement) double pointer topic
记一次jar包冲突解决过程
为抵制 7-Zip,列出 “三宗罪” ?网友:“第3个才是重点吧?”
Cocos Creator 2.x 自动打包(构建 + 编译)
Is it safe to open an account with tongdaxin?
Add color to the interface automation test framework and realize the enterprise wechat test report
Myopia: take off or match glasses? These problems must be understood clearly first
Aike AI frontier promotion (7.3)