leetcode每日一题 六月2日

题目

求 1+2+...+n ,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。
如果不加以限制 则可以采用递归的方式来做

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

利用&&的短路性质

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

你可能感兴趣的:(leetcode每日一题 六月2日)