Tuesday, July 29, 2014

[LeetCode] Pascal's Triangle 2

Java Code:


public List getRow(int rowIndex) {
       List res = new ArrayList();
       
       for(int i = 0; i <= rowIndex; i++){       
           int temp = 0;
           
           for(int j = 0; j < res.size(); j++){
               int temp1 = res.get(j);
               res.set(j, temp1 + temp);
               temp = temp1;
           }
           
           if(temp == 0){
               res.add(1);
           }else{
               res.add(temp);
           }
       }
       
       return res; 
    }

No comments:

Post a Comment