A Little Bit Long But Hopefully More Intuitive Java Solution


  • 0
    C
    public class Solution 
    {
        public List<Integer> getRow(int rowIndex)
        {
            List<Integer> ans = new ArrayList<Integer>();
            if(rowIndex == 0)
                ans.add(1);
            else if(rowIndex == 1)
            {
                ans.add(1);
                ans.add(1);
            }
            else if(rowIndex == 2)
            {
                ans.add(1);
                ans.add(2);
                ans.add(1);
            }
            else
            {
                ans.add(1);
                List<Integer> last = getRow(rowIndex - 1);
                for(int i = 1; i < rowIndex; i++)
                {
                    ans.add(last.get(i - 1) + last.get(i));
                }
                ans.add(1);
            }
            return ans;
        }
    }

Log in to reply
 

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