当前位置:网站首页>【剑指 Offer 】64. 求1+2+…+n
【剑指 Offer 】64. 求1+2+…+n
2022-07-03 16:29:00 【LuZhouShiLi】
剑指 Offer 64. 求1+2+…+n
题目
求 1+2+…+n ,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。
思路
- 将if判断语句转换成短路求值的方法来做。
- 短路求值:if(A & &B) // 如果A是True, 那么就要继续看B,A是False,没有必要看B;if(A || B) // 如果A是True,那么没有必要看B,如果A是Flase,还是要看B。
- 先写出普通的递归形式,在改写成短路求值形式
代码
- 短路求值
class Solution {
public:
int sumNums(int n) {
bool x = n > 1 && (n += sumNums(n - 1)) > 0;
return n;
}
};
- 递归形式
class Solution {
public:
int sumNums(int n) {
if(n == 1) return 1;
n += sumNums(n - 1);
return n;
}
};
边栏推荐
- 用同花顺炒股开户安全吗?
- Famous blackmail software stops operation and releases decryption keys. Most hospital IOT devices have security vulnerabilities | global network security hotspot on February 14
- Stm32f103c8t6 firmware library lighting
- QT serial port UI design and solution to display Chinese garbled code
- pycharm错Error updating package list: connect timed out
- Learn from me about the enterprise flutter project: simplified framework demo reference
- Asemi rectifier bridge umb10f parameters, umb10f specifications, umb10f package
- Deep understanding of grouping sets statements in SQL
- Everyone in remote office works together to realize cooperative editing of materials and development of documents | community essay solicitation
- [web security] - [SQL injection] - error detection injection
猜你喜欢
From the 18th line to the first line, the new story of the network security industry
Remote file contains actual operation
近视:摘镜or配镜?这些问题必须先了解清楚
Low level version of drawing interface (explain each step in detail)
[web security] - [SQL injection] - error detection injection
线程池执行定时任务
Explore Cassandra's decentralized distributed architecture
Basis of target detection (IOU)
A survey of state of the art on visual slam
arduino-esp32:LVGL项目(一)整体框架
随机推荐
Google Earth engine (GEE) - daymet v4: daily surface weather data set (1000m resolution) including data acquisition methods for each day
From the 18th line to the first line, the new story of the network security industry
Mysql 将逗号隔开的属性字段数据由列转行
[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
arduino-esp32:LVGL项目(一)整体框架
Famous blackmail software stops operation and releases decryption keys. Most hospital IOT devices have security vulnerabilities | global network security hotspot on February 14
NSQ源码安装运行过程
Pychart error updating package list: connect timed out
Threejs Part 2: vertex concept, geometry structure
Chinese translation of Tagore's floating birds (1~10)
Cocos Creator 2.x 自动打包(构建 + 编译)
PHP中register_globals参数设置
PyTorch 1.12发布,正式支持苹果M1芯片GPU加速,修复众多Bug
[redis foundation] understand redis persistence mechanism together (rdb+aof graphic explanation)
消息队列消息丢失和消息重复发送的处理策略
Expression of request header in different countries and languages
[web security] - [SQL injection] - error detection injection
Eleven requirements for test management post
The accept attribute of the El upload upload component restricts the file type (detailed explanation of the case)
SDNU_ ACM_ ICPC_ 2022_ Winter_ Practice_ 4th [individual]