MENU

剑指 Offer 14- I. 剪绳子

March 10, 2022 • offer,题解

题目详细信息:https://leetcode-cn.com/problems/jian-sheng-zi-lcof/

class Solution {
public:
    int cuttingRope(int n) {
        if (n == 1 || n == 0) return 0;
        if (n == 2) return 1;
        if (n == 3) return 2;
        if (n == 4) return 4;

        // return (((int)pow(3, n / 3 - 1)) * (n % 3)); 
        int three = n / 3;
        if (n % 3 == 1) {
            // three --;
            return ((int)pow(3, n / 3 - 1)) * 2 * 2;
        } else if (n % 3 == 2) {
            return ((int)pow(3, n / 3)) * 2;
        } else {
            return ((int)pow(3, n / 3));
        }
    }
};
作者:NorthCity1984
出处:https://grimoire.cn/offer/jian-sheng-zi-lcof.html
版权:本文《剑指 Offer 14- I. 剪绳子》版权归作者所有
转载:欢迎转载,但未经作者同意,必须保留此段声明;必须在文章中给出原文连接;否则必究法律责任

Last Modified: May 2, 2022