Here is my solution.


  • 0
    H
    public class Solution {
        public List<Integer> getRow(int rowIndex) {
            ArrayList<Integer> pre= new ArrayList<Integer>();
            ArrayList<Integer> res= new ArrayList<Integer>();
            pre.add(1);
            if(rowIndex==0)
            return pre;
            for(int n=1; n<=rowIndex; n++){
                ArrayList<Integer> next= new ArrayList<Integer>();
                next.add(1);
                for(int i=1;i<n;i++){
                    next.add(pre.get(i-1)+pre.get(i));
                }
                next.add(1);
                pre=next;
                if(n==rowIndex)
                res=next;
            }
            return res;
        }
    }

  • 1
    L

    You don't need the 'res' list.

    public class Solution {
    public List<Integer> getRow(int rowIndex) {
        ArrayList<Integer> pre= new ArrayList<Integer>();
        pre.add(1);
        if(rowIndex==0)
        return pre;
        for(int n=1; n<=rowIndex; n++){
            ArrayList<Integer> next= new ArrayList<Integer>();
            next.add(1);
            for(int i=1;i<n;i++){
                next.add(pre.get(i-1)+pre.get(i));
            }
            next.add(1);
            pre=next;
        }
        return pre;
    }
    

    }


Log in to reply
 

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