Evaluate Reverse Polish Notation

Title: Evaluate Reverse Polish Notation Source: leetcode.com Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples: ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9 ["4", "13", "5", "/", "+"] -> ...

Combination Sum

Title: Combination Sum Source: leetcode.com Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (including target) will be positive integers. Elements in a ...

Combinations

Title: Combinations Source: leetcode.com Given two integers n and k, return all possible combinations of k numbers out of 1 … n. For example, If n = 4 and k = 2, a solution is: [ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4], ] 12345678 [  [2,4],  [3,4],  [2,3],  [1,2],  [1,3],  [1,4],] Python solution (a) Python ''' https://leetcode.com/problems/combinations/ ''' class Solution(object): def ...

Binary Tree Postorder Traversal

Title: Binary Tree Postorder Traversal Source: leetcode.com Given a binary tree, return the postorder traversal of its nodes’ values. For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3 12345    1    \     2    /   3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? Python solution (Iterative) Python ''' https://leetcode.com/problems/binary-tree-postorder-traversal/ ''' # ...

Binary Tree Zigzag Level Order Traversal

Title: Binary Tree Zigzag Level Order Traversal Source: leetcode.com 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 / \ 15 7 12345 ...

Binary Tree Right Side View

Title: Binary Tree Right Side View Source: leetcode.com Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom. For example: Given the following binary tree, 1 <--- / \ 2 3 <--- \ \ 5 4 <--- 12345 ...

Word Pattern

Title: Word Pattern Source: leetcode.com Given a pattern and a string str, find if str follows the same pattern. Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str. Examples: pattern = "abba", str = "dog cat cat dog" should return true. ...

Unique Paths II

Title: Unique Paths II Source: leetcode.com Follow up for “Unique Paths”: Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one obstacle in the middle of a 3×3 ...

Rotate Image

Title: Rotate Image Source: leetcode.com You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Follow up: Could you do this in-place? Java solution Java /* https://leetcode.com/problems/rotate-image/ */ class RotateImage { public static void main(String args[]) { int[][] image = { {1,2,3,4}, {5,6,7,8}, {9,10,11,12}, {13,14,15,16} }; ...

Bulls and Cows

Title: Bulls and Cows Source: leetcode.com You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a guess, you provide a hint that indicates how many digits in said guess match your secret ...