My Java code beats 99%


  • 0
    O
    public class Solution {
        public List<Integer> getRow(int rowIndex) {
            List<Integer> result = new ArrayList<>(rowIndex + 1);
            result.add(1);
            for (int i = 1; i <= rowIndex / 2; i++) {
                result.add((int)((long)result.get(i - 1) * (rowIndex - i + 1) / i));
            }
            for (int i = rowIndex / 2 + 1; i <= rowIndex; i++) {
                result.add(result.get(rowIndex - i));
            }
            return result;
        }
    }
    

    Three points

    • The kth line of pascal triangle can be computed using following fomula.
      [C(k, 0), C(k, 1), C(k, 2) ... C(k, k)]

    • C(k, x) = C(k, x - 1) * (k - x + 1) / i (Expanding C(k, x) will get you here.)

    • The numbers in one line is symmetric. C(k, 0) = C(k, k) C(k, 1) = C(k, k - 1)


Log in to reply
 

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