My accepted Java Solution


  • 0
    M
    public class Solution {
        public List<Integer> getRow(int rowIndex) {
            LinkedList<Integer> res = new LinkedList<Integer>();
            if(rowIndex < 0) return res;
            if(rowIndex == 0){
                res.add(Integer.valueOf(1));
                return res;
            }
            
            LinkedList<Integer> prev = new LinkedList<Integer>();
            int count = 0;
            while(count<rowIndex){
                res = new LinkedList<Integer>();
                res.add(Integer.valueOf(1));
                if(prev.isEmpty()){
                    prev = res;
                    continue;
                }
                
                while(!prev.isEmpty()){
                    Integer first = prev.pop();
                    if(prev.isEmpty()) break;
                    
                    Integer second = prev.peek();
                    res.add(first + second);
                }
                
                res.add(Integer.valueOf(1)); //last 1
                count++;
                prev = res;
            }
            
            return res;
        }
    }

Log in to reply
 

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