当前位置:网站首页>Let code & lt; offer 64 +n>

Let code & lt; offer 64 +n>

2020-12-07 16:02:07 Jruing

subject : The finger of the sword Offer 64. seek 1+2+…+n

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).

Example

 Example  1:
 Input : n = 3
 Output : 6

 Example  2:
 Input : n = 9
 Output : 45

Code

class Solution:
    def sumNums(self, n: int) -> int:
        return n and (n+self.sumNums(n-1))

Ideas

Using recursive thinking and Conditional judgment and by True The logic of

source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/design-circular-queue
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .

版权声明
本文为[Jruing]所创,转载请带上原文链接,感谢
https://chowdera.com/2020/12/20201207154724052m.html