[剑指offer] JAVA版题解 面试题64

[剑指offer] JAVA版题解 面试题64_第1张图片

代码演示:

package swordfingeroffer;

/**
 * 

Description:

* * @author 罗志远 * @version 1.0 * @name InterviewQuestion64 * @date 2020-07-05 17:57 */
public class InterviewQuestion64 { public int sumFromOneToN(int n) { int sum = n; boolean b = (n > 0) && ((sum += sumFromOneToN(n - 1)) > 0); return sum; } public static void main(String[] args) { InterviewQuestion64 interviewQuestion64 = new InterviewQuestion64(); System.out.println(interviewQuestion64.sumFromOneToN(5)); } }

你可能感兴趣的:(剑指offer)