题解 | #剪绳子#
剪绳子
http://www.nowcoder.com/practice/57d85990ba5b440ab888fc72b0751bf8
public class Solution{ public int cutRope(int target){ if(target == 2 || target == 3) return target - 1; int result = 1; while(target > 4){ target -= 3; result *= 3; } return result * target; } } // public class Solution { // public int cutRope(int target) { // if (target == 2 || target == 3) // return target - 1; // int res = 1; // while (target > 4) { // //如果target大于4,我们不停的让他减去3 // target = target - 3; // //计算每段的乘积 // res = res * 3; // } // return target * res;
// } // }
我居南半坡 文章被收录于专栏
多刷题,积蓄力量,欢迎讨论
查看11道真题和解析