# Leetcode 343. Integer Break ###### tags: `Leetcode(JAVA)` 題目 : https://leetcode.com/problems/integer-break/ 。 想法 : 規律就在2跟3身上(?),把一個數字分解成2跟3的總和就好。 時間複雜度 : O(1)。 程式碼 : (JAVA) ``` class Solution { public int integerBreak(int n) { int num_3, ans; if(n == 2) return 1; else if(n == 3) return 2; if(n%3 == 0){ num_3=n/3; ans=(int)Math.pow(3,num_3); return ans; } else if(n%3 == 1){ num_3=n/3-1; ans=(int)Math.pow(3,num_3)*4; return ans; } else{ num_3=n/3; ans=(int)Math.pow(3,num_3)*2; return ans; } } } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up