My java solution using Queue


  • 0
    2

    The queue should be the right structure for this question which always takes O(k) memory.

     public class Solution {
        public List<Integer> getRow(int rowIndex) {
            
            Queue<Integer> queue = new LinkedList<Integer>();
            int n = -1;
            int num = 0;
            
            while (++n <= rowIndex) {
                
                for (int i = 0;i < num - 1;i++) {
                    queue.offer(queue.poll() + queue.peek());
                }
                
                queue.offer(1);
                num = queue.size();
            }
            
            return (List<Integer>)queue;
        }
    }

Log in to reply
 

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