Leetcode 343. Integer Break
题目链接: Integer Break
难度:Medium
题目大意:
将一个整数拆分成若干个数的和,至少拆分成两个数,求拆分后这些数乘积的最大值。
思路:
参考高赞回答,数学题。
代码
class Solution {
public int integerBreak(int n) {
if(n<=3){
return n-1;//n=1+(n-1)
}
int res=0;
int a=n/3,b=n%3;//n=3a+b
if(b==0){
res=(int)Math.pow(3,a);
}
if(b==1){
res=(int)Math.pow(3,a-1)*4;
}
if(b==2){
res=(int)Math.pow(3,a)*2;
}
return res;
}
}