[LeetCode]Number of Islands II

2015-12-25
阅读 3 分钟
6k
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 ...

[LeetCode]Number of Islands

2015-12-24
阅读 3 分钟
2.8k
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. 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 1: {代码...} Answer: 1 Example 2...

[LeetCode]Game of Life

2015-12-24
阅读 3 分钟
3.1k
According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970." Given a board with m by n cells, each cell has an initial state live (1) or dead (0). Each cell interacts with its eight ne...

[LeetCode]Binary Tree Longest Consecutive Sequence

2015-12-24
阅读 2 分钟
2.4k
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 exa...

[LeetCode]Find Median from Data Stream

2015-12-24
阅读 2 分钟
2.8k
Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value. Examples: [2,3,4] , the median is 3 [2,3], the median is (2 + 3) / 2 = 2.5 Design a data structure that supports the following two o...

[LeetCode]Best Time to Buy and Sell Stock with Cooldown

2015-12-24
阅读 3 分钟
5.9k
Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times) with the following restrictions: You may not engag...

[LeetCode]Remove Duplicate Letters

2015-12-23
阅读 3 分钟
5.6k
Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and only once. You must make sure your result is the smallest in lexicographical order among all possible results. Example:Given "bcabc"Return "abc" Given "cbacdcbc"Return "acdb"

[LeetCode]Shortest Distance from All Buildings

2015-12-23
阅读 3 分钟
7.7k
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 can...

[LeetCode]Generalized Abbreviation

2015-12-23
阅读 2 分钟
7k
Write a function to generate the generalized abbreviations of a word. Example:Given word = "word", return the following list (order does not matter): {代码...}

[LeetCode]Super Ugly Number

2015-12-23
阅读 2 分钟
4.7k
Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all prime factors are in the given prime list primes of size k. For example, [1, 2, 4, 7, 8, 13, 14, 16, 19, 26, 28, 32] is the sequence of the first 12 super ugly numbers given primes = [2, 7, 13, 19...

[LeetCode]Maximum Product of Word Lengths

2015-12-23
阅读 2 分钟
4.5k
Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. You may assume that each word will contain only lower case letters. If no such two words exist, return 0. Example 1:Given ["abcw", "baz", "foo", "bar", "xtfn", ...

[LeetCode]Walls and Gates

2015-12-22
阅读 2 分钟
3.6k
You are given a m x n 2D grid initialized with these three possible values. -1 - A wall or an obstacle. 0 - A gate. INF - Infinity means an empty room. We use the value 231 - 1 = 2147483647 to represent INF as you may assume that the distance to a gate is less than 2147483647. Fill each empty roo...