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 Search

Title: Word Search Source: leetcode.com Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cell, where “adjacent” cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once. For example, Given board ...

Word Pattern 1

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. ...

Valid Anagram

Title: Valid Anagram Source: leetcode.com Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = “anagram”, t = “nagaram”, return true. s = “rat”, t = “car”, return false. Note: You may assume the string contains only lowercase alphabets. Follow up: What if ...

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 ...

Unique Paths

Title: Unique Paths Source: leetcode.com A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked ‘Finish’ in the ...

Unique Binary Search Trees

Title: Unique Binary Search Trees Source: leetcode.com Given n, how many structurally unique BST’s (binary search trees) that store values 1…n? For example, Given n = 3, there are a total of 5 unique BST’s. 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ ...

Ugly Number II

Title: Ugly Number II Source: leetcode.com Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10 ugly numbers. Note that 1 is typically treated ...

Sudoku Solver

Title: Sudoku Solver Source: leetcode.com Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character '.'. You may assume that there will be only one unique solution. Java solution Java /* https://leetcode.com/problems/sudoku-solver/ */ class SudokuSolver { public void solveSudoku(char[][] board) { int N = board.length; ...

Sqrt

Title: Sqrt(x) Source: leetcode.com Implement int sqrt(int x). Compute and return the square root of x. Java solution Java /* https://leetcode.com/problems/sqrtx/ */ class Sqrt { public static void main(String args[]) throws Exception { Sqrt solution = new Sqrt(); System.out.println(solution.mySqrt(2147395599)); } public int mySqrt(int x) throws Exception { if(x==0 || x==1) return x; long lo = ...