Category Archives: Stack and Queue

Reverse Nodes in k-Group

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is. You may … Continue reading

Posted in Array and linked list, Stack and Queue | Leave a comment

Valid Parentheses

Given a string containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[‘ and ‘]’, determine if the input string is valid. The brackets must close in the correct order, “()” and “()[]{}” are all valid but “(]” and “([)]” are not. C++: 01 class Solution { 02 public: 03     bool isValid(string s) { 04      … Continue reading

Posted in Stack and Queue | Leave a comment

Longest Valid Parentheses

Given a string containing just the characters ‘(‘ and ‘)’, find the length of the longest valid (well-formed) parentheses substring. For “(()”, the longest valid parentheses substring is “()”, which has length = 2. Another example is “)()())”, where the longest valid parentheses substring is “()()”, which … Continue reading

Posted in Array and linked list, Stack and Queue, String | Leave a comment

Multiply Strings

Given two numbers represented as strings, return multiplication of the numbers as a string. Note: The numbers can be arbitrarily large and are non-negative. C++: 001 string shift(string s,int t) 002 { 003     if(s==“0”) 004         return s; 005  … Continue reading

Posted in Number trick, Stack and Queue, String | Leave a comment

Rotate List

Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given 1->2->3->4->5->NULL and k = 2, return 4->5->1->2->3->NULL. C++: 01 /** 02  * Definition for singly-linked list. 03  * struct ListNode { 04  *     int val; 05  *     ListNode … Continue reading

Posted in Array and linked list, Stack and Queue | Leave a comment

Maximal Rectangle

Given a 2D binary matrix filled with 0’s and 1’s, find the largest rectangle containing all ones and return its area. C++: 01 class Solution { 02 public: 03     int maximalRectangle(vector<vector<char> > &matrix) { 04         // Start typing … Continue reading

Posted in Array and linked list, Stack and Queue | Leave a comment

Largest Rectangle in Histogram

Given n non-negative integers representing the histogram’s bar height where the width of each bar is 1, find the area of largest rectangle in the histogram. Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3]. C++: 01 class … Continue reading

Posted in Array and linked list, Stack and Queue | Leave a comment

Binary Tree Inorder Traversal

Given a binary tree, return the inorder traversal of its nodes’ values. For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? C++: 01 /** 02  * Definition for binary tree 03  * … Continue reading

Posted in Stack and Queue, Tree | Leave a comment

Binary Tree Level Order Traversal

Given a binary tree, return the level order traversal of its nodes’ values. (ie, from left to right, level by level). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 return its level order traversal as: [ … Continue reading

Posted in Stack and Queue, Tree | Leave a comment

Binary Tree Zigzag Level Order Traversal

Given a binary tree, return the zigzag level order traversal of its nodes’ values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ … Continue reading

Posted in Stack and Queue, Tree | Leave a comment