JAVA solution with array


  • 0
    Y
    public class Solution {
        public List<Integer> getRow(int rowIndex) {
            int[] res = null;
            List<Integer> list = new ArrayList<Integer>();
            for(int i=0; i<=rowIndex; i++){
                int[] layer = new int[i + 1];
                for(int j=0; j<=i; j++){
                    if(res == null)
                        layer[j] = 1;
                    else{
                        int left = 0;
                        int right = 0;
                        if(j-1 >= 0)
                            left = res[j-1];
                        if(j < res.length)
                            right = res[j];
                        layer[j] = left + right;
                    }
                }
                res = layer;
            }
            for(int num: res)
                list.add(num);
            return list;
        }
    }
    

Log in to reply
 

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