Sword refers to Offer 64-seeking 1+2+...+n C++

Title description

Insert picture description here

Short-circuit of solution and operation

A && B, if the value of A statement is false/0, B will not be executed . It was used in the judgment before, only to find that it can be a single sentence

class Solution {
    
    
public:
    int sumNums(int n) {
    
    
        int ans = 0;
        n && (ans = sumNums(n - 1) + n);
        return ans;
    }
};

Insert picture description here

Time complexity O(N)
Space complexity O(N)

Guess you like

Origin blog.csdn.net/qq_42883222/article/details/112792484