随笔分类 - LeetCode
摘要:Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.Note: You can only move either down or right at any point in time.思考:DP方程:dp[i][j]=grid[i][j]+min(dp[i-1][j],dp[i][j-1])。class Solution {public: int...
阅读全文
摘要:Implement regular expression matching with support for '.' and '*'.'.' Matches any single character.'*' Matches zero or more of the preceding element.The matching should cover the entire input string (not partial).The function prototype should be:bool isMatch(const ch
阅读全文
摘要:Write a function to find the longest common prefix string amongst an array of strings.思考:依次比较字符串的第i个字符,不同退出循环。class Solution {public: string longes...
阅读全文
摘要:Givens1,s2,s3, find whethers3is formed by the interleaving ofs1ands2.For example,Given:s1="aabcc",s2="dbbca",Whens3="aadbbcbcac", return true.Whens3="aadbbbaccc", return false.class Solution {private: bool f[1000][1000];public: bool isInterleave(string s1, str
阅读全文
摘要:Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.click to show follow up.Follow up:Did you use extra space? A straight forward solution using O(mn) space is probably a bad idea. A simple improvement uses O(m + n) space, but still not the best solution. Cou
阅读全文
摘要:Follow up for "Remove Duplicates": What if duplicates are allowed at most twice?For example, Given sorted array A = [1,1,1,2,2,3],Your function should return length = 5, and A is now [1,1,2,2,3].思考:双指针,A[i]与A[cur]前两个值比较。class Solution {public: int removeDuplicates(int A[], int n) { // IMPO
阅读全文
摘要:Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists./** * Def...
阅读全文
摘要:You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?思考:DP,斐波拉契。class Solution {public: int climbStairs(int n) { // IMPORTANT: Please reset any member data you declared, as ...
阅读全文
摘要:Given a collection of intervals, merge all overlapping intervals.For example, Given [1,3],[2,6],[8,10],[15,18], return [1,6],[8,10],[15,18].思考:先排序。比较相邻两个interval,left为前一个start,若后一个start小于等于前一个,说明这两个interval需要合并,right为两个end较大者。若后一个start大于前一个end,说明这两个interval不需要合并,输出前一个。/** * Definition for an interva
阅读全文
摘要:Given a sorted array of integers, find the starting and ending position of a given target value.Your algorithm's runtime complexity must be in the order of O(log n).If the target is not found in the array, return [-1, -1].For example, Given [5, 7, 7, 8, 8, 10] and target value 8, return [3, 4].思
阅读全文
摘要: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 =[
阅读全文
摘要:Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.If the last word does not exist, return 0.Note: A word is defined as a character sequence consists of non-space characters only.For example, Given s = "He
阅读全文
摘要:Implement pow(x, n).思考:一开始想的太复杂了,跟这一题联系起来了(链接)。后来看到返回值是double型,所以此题就没有那么复杂了。毕竟面试题不会要求写一大堆代码的,最重要的还是考察算法思想:快速幂取模,当然这里不用取模。class Solution {public: double pow(double x, int n) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for...
阅读全文
摘要:Implement int sqrt(int x).Compute and return the square root of x.思考:参考链接:http://www.cnblogs.com/pkuoliver/archive/2010/10/06/sotry-about-sqrt.htmlclass Solution {public: int sqrt(int x) { // Start typing your C/C++ solution below // DO NOT write int main() function double an...
阅读全文
摘要:Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3].The largest rectangle is shown in the shaded area, which has
阅读全文
摘要: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 7return its zigzag level order traversal as:[ [...
阅读全文
摘要:Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).For example: Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7return its level order traversal as:[ [3], [9,20], [15,7]]confused what "{1,#,2,3}" means? > rea.
阅读全文
摘要:There are N children standing in a line. Each child is assigned a rating value.You are giving candies to these children subjected to the following requirements:Each child must have at least one candy.Children with a higher rating get more candies than their neighbors.What is the minimum candies you
阅读全文
摘要:Given an array of integers, every element appears three times except for one. Find that single one.Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?思考:参考这里。class Solution {public: int singleNumber(int A[], int n) { // IMPORTANT...
阅读全文
摘要:Given an array of integers, every element appears twice except for one. Find that single one.Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?思考:位运算。class Solution {public: int singleNumber(int A[], int n) { // IMPORTANT: Pleas...
阅读全文