美文网首页
剑指 Offer 14- II. 剪绳子 II

剑指 Offer 14- II. 剪绳子 II

作者: bangbang2 | 来源:发表于2020-08-27 07:59 被阅读0次
    image.png

    这也没啥,只不过把int变为BigInteger

    import java.math.BigInteger;
    class Solution {
        public int cuttingRope(int n) {
          BigInteger [] dp=new BigInteger[n+1];
         
          if(n==2) return 1;
          if(n==3)   return 2;
                  dp[1]=BigInteger.ONE;
                  dp[2]=BigInteger.valueOf(2);
                  dp[3]=BigInteger.valueOf(3);
            for(int i=4;i<=n;i++){
                 dp[i]=BigInteger.ZERO;
              for(int j=1;j<i;j++){
               
                  
                  BigInteger temp=dp[j].multiply(dp[i-j]);
                  if(dp[i].compareTo(temp)<0) dp[i]=temp;
                 
              }
          }
          return dp[n].mod(BigInteger.valueOf(1000000007)).intValue();
        }
    }
    

    相关文章

      网友评论

          本文标题:剑指 Offer 14- II. 剪绳子 II

          本文链接:https://www.haomeiwen.com/subject/oydhdktx.html