当前位置:网站首页>剑指 Offer 64. 求1+2+…+n,逻辑运算符短路效应
剑指 Offer 64. 求1+2+…+n,逻辑运算符短路效应
2022-07-28 21:53:00 【愈努力俞幸运】
剑指 Offer 64. 求1+2+…+n
https://leetcode.cn/problems/qiu-12n-lcof/
解题思路参见
面试题64. 求 1 + 2 + … + n(逻辑符短路,清晰图解) - 求1+2+…+n - 力扣(LeetCode)
https://leetcode.cn/problems/qiu-12n-lcof/solution/mian-shi-ti-64-qiu-1-2-nluo-ji-fu-duan-lu-qing-xi-/常见的逻辑运算符有三种,即 “与 and ”,“或 or ”,“非 not ” ;而其有重要的短路效应,如下所示:
if(A and B) 若 A 为 false ,则 B 的判断不会执行(即短路),直接判定 A and B 为 false
if(A or B) 若 A 为 true ,则 B 的判断不会执行(即短路),直接判定 A or B 为 true
class Solution:
def sumNums(self, n):
def digui(n):
n>1 and digui(n-1)
self.res+=n
self.res=0
digui(n)
return self.res
a=Solution()
print(a.sumNums(3))
print(a.sumNums(9))边栏推荐
- What if win11 cannot find the DNS address? Win11 can't find DNS and can't access the web page solution
- wget什么意思
- The front mounted ADAS camera in parking increased by 54.15% year-on-year, with TOP10 suppliers taking the lead
- Crud of MySQL
- The development mode of digital retail dominated by traffic is only the beginning
- Meet the outbreak period with the integration of transportation and parking, rush for mass production or build a technical moat?
- Arduino框架下STM32F103C系列单片机引脚映射关系
- 如何将一个mongodb中集合的索引 添加到另一个mongodb中集合中
- Zero vision technology completed the pre-A round of financing and promoted the domestic replacement of intelligent driving platform software
- 类中多函数填写,LeetCode919——完全二叉树插入器
猜你喜欢

trivy【3】自定义扫描策略

2022 R2 mobile pressure vessel filling test question simulation test platform operation

Wechat applet development ④

2022 G2 power plant boiler stoker examination question bank simulated examination platform operation

机器学习问题笔记

Messages from students participating in the competition: memories of the 17th session

MySQL introduction

Pin mapping relationship of stm32f103c series single chip microcomputer under Arduino framework

深度剖析集成学习Adaboost

Crud of MySQL
随机推荐
字节8年女测试总监工作感悟—写给想转行或即将进入测试行业的女生们...
通过Wi-Fi 7实现极高吞吐量——洞察下一代Wi-Fi物理层
一文读懂Okaleido Tiger近期动态,挖掘背后价值与潜力
金仓数据库KingbaseES客户端编程接口指南-ODBC(5. 开发过程)
类中多函数填写,LeetCode919——完全二叉树插入器
The front mounted ADAS camera in parking increased by 54.15% year-on-year, with TOP10 suppliers taking the lead
With the "integration of driving and parking", freytek's high-performance domain controller leads the new track
金仓数据库 KingbaseES 与 Oracle 的兼容性说明(4. SQL)
mongodb索引添加、查看、导出、删除
深度剖析集成学习Xgboost
Mycms we media mall V3.6 release, compatible with micro engine application development (laravel framework)
Typescript class method this pointer binding
2022 R2 mobile pressure vessel filling test question simulation test platform operation
1314_ Serial port technology_ Basic information of RS232 communication
22牛客多校day1 I - Chiitoitsu 概论dp
以流量为主导的数字零售的发展模式,仅仅只是一个开始
Function function
从XSS Payload学习浏览器解码
Routeros limited DNS hijacking and check
Zero vision technology completed the pre-A round of financing and promoted the domestic replacement of intelligent driving platform software