Java O(N) time O(N) space solution


  • 0
    Z
    public class Solution {
        public List<Integer> getRow(int rowIndex) {
            if (rowIndex<0)
                return null;
            List<Integer> result = new ArrayList<Integer>(rowIndex+1);
            result.add(1);
            int i=1,j=rowIndex;
            while (i<j) {
                result.add((int)(1.0*result.get(i-1)*j/i));
                i++;
                j--;
            }
            for (;i<=rowIndex;i++) {
                result.add(result.get(rowIndex-i));
            }
            return result;
        }
    }
    

Log in to reply
 

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