当前位置:网站首页>[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;
}
};
边栏推荐
- EditText request focus - EditText request focus
- Add color to the interface automation test framework and realize the enterprise wechat test report
- Yu Wenwen, Hu Xia and other stars take you to play with the party. Pipi app ignites your summer
- AcWing 第58 场周赛
- Rk3399 platform development series explanation (WiFi) 5.54. What is WiFi wireless LAN
- 疫情常态化大背景下,关于远程办公的思考|社区征文
- Deep understanding of grouping sets statements in SQL
- Chinese translation of Tagore's floating birds (1~10)
- 记一次jar包冲突解决过程
- Aike AI frontier promotion (7.3)
猜你喜欢

【LeetCode】94. Middle order traversal of binary tree

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

A survey of state of the art on visual slam

Mb10m-asemi rectifier bridge mb10m

QT serial port UI design and solution to display Chinese garbled code

深入理解 SQL 中的 Grouping Sets 语句

Google Earth engine (GEE) - daymet v4: daily surface weather data set (1000m resolution) including data acquisition methods for each day

一台服务器最大并发 tcp 连接数多少?65535?

Mysql 将逗号隔开的属性字段数据由列转行

NSQ source code installation and operation process
随机推荐
Cocos Creator 2.x 自动打包(构建 + 编译)
What is the maximum number of concurrent TCP connections for a server? 65535?
Pyinstaller is not an internal or external command, nor is it a runnable program or batch file
EditText request focus - EditText request focus
AcWing 第58 场周赛
Google Earth engine (GEE) - daymet v4: daily surface weather data set (1000m resolution) including data acquisition methods for each day
8 tips for effective performance evaluation
"Everyday Mathematics" serial 56: February 25
如何在本机搭建SVN服务器
中南大学|通过探索理解: 发现具有深度强化学习的可解释特征
Two sides of the evening: tell me about the bloom filter and cuckoo filter? Application scenario? I'm confused..
高等数学(第七版)同济大学 习题2-1 个人解答
Everyone in remote office works together to realize cooperative editing of materials and development of documents | community essay solicitation
Remote file contains actual operation
Mb10m-asemi rectifier bridge mb10m
How to initialize views when loading through storyboards- How is view initialized when loaded via a storyboard?
Golang 装饰器模式以及在NSQ中的使用
Batch files: list all files in a directory with relative paths - batch files: list all files in a directory with relative paths
Expression of request header in different countries and languages
NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线