leetcode 343

2018-12-20  本文已影响0人  Ariana不会哭
图片.png 图片.png

C++

int integerBreak(int n) {
        vector<int> dp={0,0,1,2,4,6,9};
        for(int i=7;i<=n;i++)
        {
            dp.push_back(3*dp[i-3]);
        }
        return dp[n];
    }

Java

public int integerBreak(int n) {
        if (n == 2)
            return 1;
        if (n == 3)
            return 2;
            
        int[] dp = new int[n+1];
        dp[2] = 2;
        dp[3] = 3;
        dp[4] = 4;
        for (int i = 5; i <= n; i++) {
                dp[i] = dp[i-3] * 3;
            }    
        
            return dp[n];
    }
上一篇下一篇

猜你喜欢

热点阅读