204ms Java Solution


  • 0
    A
    public class Solution {
        public List<List<Integer>> generate(int numRows) {
            List<List<Integer>> result = new ArrayList<List<Integer>>();
            List<Integer> current;
            List<Integer> temp;
            int prev;
            
            if(numRows==0)
                return result;
            
            current = new ArrayList<Integer>();
            current.add(1);
            result.add(current);
            if(numRows==1)
                return result;
            
            current = new ArrayList<Integer>();
            current.add(1);
            current.add(1);
            result.add(current);
            if(numRows==2)
                return result;
            
            numRows-=2;
            while(numRows > 0){
                current = new ArrayList<Integer>();
                prev=-1;
                temp = result.get(result.size()-1);
                for(int element : temp){
                    if(element==1){
                        if(prev==-1)
                        {
                            prev=1;
                            current.add(1);
                            continue;
                        }
                        current.add(prev+element);
                        current.add(1);
                        result.add(current);
                        prev=-1;
                        break;
                    }
                    
                    current.add(prev+element);
                    prev = element;
                    continue;
                }
                numRows--;
            }
            return result;
        }
    }

Log in to reply
 

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