Yearly Archives: 2015


[leetcode] Course Schedule

Course Schedule There are a total of n courses you have to take, labeled from 0 to n – 1. Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1] Given the total number of courses and a list of prerequisite pairs, […]


[leetcode] Wildcard Matching

Wildcard Matching Implement wildcard pattern matching with support for ‘?’ and ‘*’. ‘?’ Matches any single character. ‘*’ Matches any sequence of characters (including the empty sequence). The matching should cover the entire input string (not partial). The function prototype should be: bool isMatch(const char *s, const char *p) Some examples: isMatch(“aa”,”a”) → […]


[leetcode] Summary Ranges

Given a sorted integer array without duplicates, return the summary of its ranges. For example, given [0,1,2,4,5,7], return [“0->2″,”4->5″,”7”]. Credits: Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.     class Solution { public: vector<string> summaryRanges(vector<int>& nums) { vector<string> ans; string range; if(nums.size() == 0) […]


[leetcode] Sliding Window Maximum

Sliding Window Maximum Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position. For example, Given nums = [1,3,-1,-3,5,3,6,7], and k = 3. Window […]


[leetcode] Word Search II

Word Search II Given a 2D board and a list of words from the dictionary, find all words in the board. Each word must 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 […]


[leetcode] Shortest Word Distance I && II && III

Shortest Word Distance I Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list. For example, Assume that words = [“practice”, “makes”, “perfect”, “coding”, “makes”]. Given word1 = “coding”, word2 = “practice”, return 3. Given word1 = “makes”, word2 = “coding”, return 1. Note: You may assume that word1 does not equal to word2, and word1 and word2 are both in […]


[leetcode] Game of Life

Game of Life 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). […]