【剑指offer】【贪心】14- II. 剪绳子

题目链接:https://leetcode-cn.com/problems/jian-sheng-zi-ii-lcof/

贪心

class Solution {
public:
    int cuttingRope(int n) {
        if(n <= 3) return 1 *(n - 1);

        long long res = 1;
        if(n % 3 == 1) res = 4, n -= 4;
        else if(n % 3 == 2) res = 2, n -= 2;

        while(n) res *= 3, res %= 1000000007, n -= 3;
        
        return res % (1000000007);
    }
};
上一篇:2020-11-24


下一篇:为什么父类引用可以指向子类对象 子类引用不能指向父类对象 泛型