Java AC Solution


  • 0
    A
    public List<Integer> getRow(int rowIndex) {
            if (rowIndex == 0) {
                return Arrays.asList(1);
            } else if (rowIndex == 1) {
                return Arrays.asList(1, 1);
            } else {
                List<Integer> result = new ArrayList<>(Collections.nCopies(rowIndex + 1, 0));
                result.set(0, 1);
                result.set(1, 1);
    
                for (int row = 2; row <= rowIndex; row++) {
                    for (int i = row; i >= 1; i--) {
                        result.set(i, result.get(i) + result.get(i - 1));
                    }
                }
    
                return result;
            }
        }
    

Log in to reply
 

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