This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand operation which turns the water at position (row, col) into a land. Given a list of positions to operate, count the number of islands after each addLand operation. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water. | |
// Example: | |
// Given m = 3, n = 3, positions = [[0,0], [0,1], [1,2], [2,1]]. | |
// Initially, the 2d grid grid is filled with water. (Assume 0 represents water and 1 represents land). | |
// 0 0 0 | |
// 0 0 0 | |
// 0 0 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down). | |
// Find all strobogrammatic numbers that are of length = n. | |
// For example, | |
// Given n = 2, return ["11","69","88","96"]. | |
public List<String> findStrobogrammatic(int n) { | |
return helper(n, n); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// You want to build a house on an empty land which reaches all buildings in the shortest amount of distance. You can only move up, down, left and right. You are given a 2D grid of values 0, 1 or 2, where: | |
// Each 0 marks an empty land which you can pass by freely. | |
// Each 1 marks a building which you cannot pass through. | |
// Each 2 marks an obstacle which you cannot pass through. | |
// For example, given three buildings at (0,0), (0,4), (2,2), and an obstacle at (0,2): | |
// 1 - 0 - 2 - 0 - 1 | |
// | | | | | | |
// 0 - 0 - 0 - 0 - 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Given a non-empty binary search tree and a target value, find the value in the BST that is closest to the target. | |
// Note: | |
// Given target value is a floating point. | |
// You are guaranteed to have only one unique value in the BST that is closest to the target. | |
// use successor and predecessor | |
public int closestValue(TreeNode root, double target) { | |
TreeNode successor = null, predecessor = null; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Given a string, find the length of the longest substring T that contains at most k distinct characters. | |
// For example, Given s = “eceba” and k = 2, | |
// T is "ece" which its length is 3. | |
public int lengthOfLongestSubstringKDistinct(String s, int k) { | |
if(s == null || s.length() == 0) return 0; | |
int max = 0; | |
Map<Character, Integer> map = new HashMap<Character, Integer>(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down). | |
// Write a function to determine if a number is strobogrammatic. The number is represented as a string. | |
// For example, the numbers "69", "88", and "818" are all strobogrammatic. | |
public boolean isStrobogrammatic(String num) { | |
if(num == null || num.length() == 0) return false; | |
int left = 0, right = num.length() - 1; | |
while(left <= right){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Given a binary tree, find the length of the longest consecutive sequence path. | |
// The path refers to any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The longest consecutive path need to be from parent to child (cannot be the reverse). | |
// For example, | |
// 1 | |
// \ | |
// 3 | |
// / \ | |
// 2 4 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 <= i < j < k < n that satisfy the condition nums[i] + nums[j] + nums[k] < target. | |
// For example, given nums = [-2, 0, 1, 3], and target = 2. | |
// Return 2. Because there are two triplets which sums are less than 2: | |
// [-2, 0, 1] | |
// [-2, 0, 3] | |
// Follow up: | |
// Could you solve it in O(n2) runtime? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black pixels are connected, i.e., there is only one black region. Pixels are connected horizontally and vertically. Given the location (x, y) of one of the black pixels, return the area of the smallest (axis-aligned) rectangle that encloses all black pixels. | |
// For example, given the following image: | |
// [ | |
// "0010", | |
// "0110", | |
// "0100" | |
// ] | |
// and x = 0, y = 2, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), write a function to find the number of connected components in an undirected graph. | |
// Example 1: | |
// 0 3 | |
// | | | |
// 1 --- 2 4 | |
// Given n = 5 and edges = [[0, 1], [1, 2], [3, 4]], return 2. | |
// Example 2: | |
// 0 4 |