๐ŸŒ— 343. ๆ•ดๆ•ฐๆ‹†ๅˆ†

ๅžไฝ›็ซฅๅญ2022ๅนด6ๆœˆ9ๆ—ฅๅฐไบŽ 1 ๅˆ†้’Ÿ

๐ŸŒ— 343. ๆ•ดๆ•ฐๆ‹†ๅˆ†

้šพๅบฆ: ๐ŸŒ—

้—ฎ้ข˜ๆ่ฟฐ

img_10.png


่งฃๆณ•

class Solution {
    public int integerBreak(int n) {
        // ๆ€่ทฏ๏ผš
        // ๅฐฝๅฏ่ƒฝๆ‹†ๆˆ 3 ็š„ๅ€ๆ•ฐ
        if(n ==  2) {
            return 1;
        }
        if(n == 3) {
            return 2;
        }
        if(n == 4) {
            return 4;
        }
        // n > 4
        int count = n / 3;
        int left = n % 3;
        if(left == 0) {
            return (int)Math.pow(3, count);
        } else if(left == 1) {
            return (int)Math.pow(3, count - 1) * 4;
        } else if(left == 2) {
            return (int)Math.pow(3, count) * 2;
        }
        return 0;
    }
}

่พ“ๅ‡บ

img_11.png

ไธŠๆฌก็ผ–่พ‘ไบŽ: 2022/6/20 ไธ‹ๅˆ8:24:47
่ดก็Œฎ่€…: liuxianzhishou