与众不同的 java解法 带中文注释


  • 0
    G

    ···
    public class Solution {
    public int numWays(int n, int k) {
    if(n==0){
    return 0;
    }
    int maxTwo = n/2;//最多有多少块相邻同色的板子,比如一共有8块板子,最多有4块相邻同色的板子。
    int count = 0;

        for(int i=0;i<=maxTwo;i++){
            double single = n-i*2; //每块都不相同颜色的数目
            double shumu = n-i;  //把两块相邻的板子看成一块,一共有多少块
            
            double chafa = gailv(shumu,single)/jiecheng(i); // 一共有多少种插法,
            
            
            count += (int)k*Math.pow(k-1,shumu-1)*chafa; 
            
        }
        
        return count;
    }
    
    public double jiecheng(double a){
        double res = 1;
        for(double i=a;i>1;i--){
            res = res*i;
        }
        return res;
    }
    
    public double gailv(double a,double b){
        double res = 1;
        for(double i=a;i>b;i--){
            res = res*i;
        }
        return res;
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.