0ms solution and beat 99.90% submit (JAVA)


  • 0
    I

    Here is my solution and explanation:

        public List<Integer> getRow(int rowIndex) {
            ArrayList<Integer> result = new ArrayList<>();
            double s = 1;
            for(int i = 0;i<=rowIndex;i++){
                if(i==0||i==rowIndex){
                    result.add(1);
                    continue;
                }
               // why do this math??
                s = (rowIndex-i+1)*s/i; 
                result.add((int)s);
            }
            return result;
        }
    

    and here is my explanation:

    1. why s use double type : first I use int ,but when rowindex gets to 30 ,when index gets to 15,s will overflow , so ,I use double.

    But I was confused with the line that I comment,why this will calculate the right index ?


Log in to reply
 

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