LeetCode
周大侠
这个作者很懒,什么都没留下…
展开
-
[LeetCode] Gray Code
Gray Code My Submissions Question Solution Total Accepted: 41835 Total Submissions: 125744 Difficulty: Medium The gray code is a binary numeral system where two successive values differ in only原创 2015-09-16 20:42:05 · 307 阅读 · 0 评论 -
[LeetCode]Longest Valid Parentheses(!!!)
Given a string containing just the characters ‘(’ and ‘)’, find the length of the longest valid (well-formed) parentheses substring. For “(()”, the longest valid parentheses substring is “()”,原创 2015-07-14 22:54:55 · 333 阅读 · 0 评论 -
[LeetCode] Subsets(!!!!!回溯&&迭代&&位操作)(to be updated)
Subsets Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets.原创 2015-08-23 22:58:07 · 565 阅读 · 0 评论 -
[LeetCode]Permutations II(!!!!!DFS递归&&回溯)
Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example, [1,1,2] have the following unique permutations: [1,1,2], [1,2,1], and [2,1原创 2015-07-19 15:26:25 · 371 阅读 · 0 评论 -
[LeetCode] Edit Distance(!!!!!DP)
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.) You have the following 3 opera原创 2015-08-27 11:13:30 · 320 阅读 · 0 评论 -
[LeetCode]Climbing Stairs
Climbing Stairs 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,到达原创 2015-08-23 00:49:14 · 270 阅读 · 0 评论 -
[LeetCode] Maximal Square(!!!!DP优化)
定义一个D[i][j] 表示的是以i,j为右下角的最大子正方形的边长。这个边长是受限于D[i-1][j],D[i][j-1] ,D[i-1][j-1]中最小的那个的。class Solution {public: int maximalSquare(vector<vector<char>>& A) { int m = A.size(); if(m==0) retu原创 2015-08-27 20:13:28 · 346 阅读 · 0 评论 -
[LeetCode] Permutation Sequence
Permutation Sequence Total The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the permutations in order, We get the following sequence (ie, for原创 2015-08-20 22:09:22 · 312 阅读 · 0 评论 -
[LeetCode] Combinations
Combinations Given two integers n and k, return all possible combinations of k numbers out of 1 … n. For example, If n = 4 and k = 2, a solution is: [ [2,4], [3,4], [2,3], [1,2]原创 2015-08-23 22:27:40 · 258 阅读 · 0 评论 -
[LeetCode] Maximal Rectangle(!!!DP)
关于最大某某问题,使用DP思想来求解,在最大子问题系列中有较为详细的解答。 Maximal Rectangle Given a 2D binary matrix filled with 0’s and 1’s, find the largest rectangle containing all ones and return its area. (实在不能理解为什么要用char)c原创 2015-08-27 16:08:06 · 561 阅读 · 0 评论 -
[LeetCode] Set Matrix Zeroes (!!!!时间空间复杂度)
Set Matrix Zeroes 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原创 2015-08-27 14:40:47 · 356 阅读 · 0 评论 -
动态规划DP
动态规划最重要的是建立状态方程 有一些非常经典的dp题原创 2015-07-19 21:26:19 · 329 阅读 · 0 评论 -
[LeetCode]Anagrams
题目的意思是将所有的满足anagrams的输出 判断满足anagrams:将两个string sort,如果相等则是使用一个mapclass Solution {public: vector<string> anagrams(vector<string>& strs) { map<string,int> map_str; set<string> set_s原创 2015-07-19 20:35:30 · 341 阅读 · 0 评论 -
[leetCode] Pow(x,n)(分治)
“` double myPow2(double x, int n) { double tmp; if(n==1) return x; tmp=myPow2(x,n/2); if(n%2==1) //奇数 return x*tmp*tmp; else return tmp*tmp;} doubl原创 2015-07-19 16:49:52 · 300 阅读 · 0 评论 -
[LeetCode]Jump Games(贪心!!!)
Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. De原创 2015-07-19 23:02:25 · 314 阅读 · 0 评论 -
[LeetCode]maxSubArray(最大子序列和!!!!)
经典dp问题 有两种问法, 1、如果最大子序列和是负数,则输出最大的负数 2、如果最大子序列和是负数,则输出0第一个和第二个问题其实可以统一,如果输出的最大数为负数,将其变成0即可。动态方程: 最大子序列和是连续的子序列 ThisSum[i]表示第i处,以A[i]结尾的子序列的最大和。 则状态方程为ThisSum[i]=max(ThisSum[i-1]+nums[i],nums[i])也原创 2015-07-19 22:17:52 · 4107 阅读 · 0 评论 -
[LeetCode]Rotate the image(在位操作!!)
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Follow up: Could you do this in-place?在位操作说明不能开辟新的空间void rotate(int** matrix, int r, int c原创 2015-07-19 16:02:07 · 332 阅读 · 0 评论 -
[LeetCode]Jump Game II(贪心&&DP!!!!!)
Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Yo原创 2015-07-18 13:27:47 · 866 阅读 · 0 评论 -
[LeetCode] Largest Rectangle in Histogram
Largest Rectangle in Histogram 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.原创 2015-09-01 10:51:00 · 301 阅读 · 0 评论 -
最大子段和||最大子矩阵和||最大全1子矩阵||最大全1子正方形||
最大子段和 给定n个整数组成的序列A[0,1,…,n-1],求该序列子段的最大和。算法一枚举所有可能的左右边界l,r,计算sum[l,...r],算法复杂度为O(n^3)算法二sum[l,...r]的值可以由sum[0,...r]-sum[0,...l]得到。在O(n)内我们可以得到sum[0,..,n],这样只有r,l可以变化,算法复杂度为O(n^2),这是典型的空间换时间。算法三(DP)我原创 2015-08-18 19:15:55 · 3287 阅读 · 0 评论 -
[LeetCode] Decode Ways(!!!DP)
Decode Ways My Submissions Question Solution Total Accepted: 47576 Total Submissions: 290697 Difficulty: Medium A message containing letters from A-Z is being encoded to numbers using the follow原创 2015-09-16 13:24:11 · 314 阅读 · 0 评论 -
[LeetCode]Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left subtree of a node contains only nodes with keys less than the node's key.The right原创 2015-09-29 11:10:23 · 237 阅读 · 0 评论 -
[LeetCode] Binary Tree Inorder Traversal
Binary Tree Inorder Traversal My Submissions Question Solution Total Accepted: 85535 Total Submissions: 232962 Difficulty: Medium Given a binary tree, return the inorder traversal of its nodes’原创 2015-09-26 22:58:56 · 257 阅读 · 0 评论 -
[LeetCode]Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.Do not allocate extra space for another array, you must do this in place with cons原创 2015-08-23 00:47:06 · 254 阅读 · 0 评论 -
[LeetCode]Minimum Path Sum
Minimum Path Sum 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.思路还是最基本的DP,class Solution {原创 2015-08-21 23:02:45 · 265 阅读 · 0 评论 -
[LeetCode] Unique Paths
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 rea原创 2015-08-21 14:34:53 · 271 阅读 · 0 评论 -
[LeetCode] Unique Binary Search Trees(!!DP)
Unique Binary Search Trees 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原创 2015-09-27 13:38:08 · 253 阅读 · 0 评论 -
[LeetCode] Unique Paths II
Unique Paths II 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 a原创 2015-08-21 22:47:02 · 301 阅读 · 0 评论 -
[LeetCode]Restore IP Addresses(!!!!BackTrace)
Restore IP Addresses My Submissions Question Solution Total Accepted: 41299 Total Submissions: 192823 Difficulty: Medium Given a string containing only digits, restore it by returning all原创 2015-09-26 22:08:57 · 381 阅读 · 0 评论 -
[LeetCode] Rotate List
Rotate List Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given 1->2->3->4->5->NULL and k = 2, return 4->5->1->2->3->NULL.题目的意思先梳理下,如果list的长原创 2015-08-21 12:31:05 · 289 阅读 · 0 评论 -
[LeetCode] Clone Graph(!!!!graph&dfs&bfs)
Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ’s undirected graph serialization: Nodes are labeled uniquely. We use # a原创 2015-09-10 22:37:06 · 330 阅读 · 0 评论 -
[LeetCode]反转链表
迭代的方法ListNode* ReverseList2(ListNode* pHead){ ListNode* pNode=pHead;//当前结点 ListNode* pPrev=NULL;//当前结点的前一个结点 while(pNode!=NULL) { ListNode* pNext=pNode->m_pNext; pNode-原创 2015-07-13 10:55:16 · 679 阅读 · 0 评论 -
[LeetCode] Reverse Linked List II(!!!)
Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL.原创 2015-09-05 14:58:09 · 266 阅读 · 0 评论 -
[LeetCode]Permutations(回溯&&DFS&&递归!!!!)
题目意思很直白,求出不重复的所有排列,这里的是数字,对于字符串同样适用。 思路一:递归,每次都按照nums的顺序访问并且加入当前的vector,再递归的下一层。如果当前层的vector的大小==nums的大小,则是一个合理的排列。否则,按顺序遍历nums的每一个元素,如果该元素没有被加入vector则加入继续递归。所以这里需要有个标记是否访问过的visted数组,加入vector时设为true表示原创 2015-07-18 20:03:09 · 1205 阅读 · 0 评论 -
[LeetCode] Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start time原创 2015-08-19 16:11:43 · 334 阅读 · 0 评论 -
[LeetCode] Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example, Given 1->2->3->3->4->4->5, return 1->2->5. Given原创 2015-09-09 21:13:12 · 267 阅读 · 0 评论 -
[LeetCode]Merge Intervals
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]./** * Definition for an interval. * struct Interv原创 2015-08-18 23:05:41 · 417 阅读 · 0 评论 -
[LeetCode]Valid Sodoku
判断一个数独是否是有效的 1、每一行1-9只出现一次 2、每一列1-9只出现一次 3、每一个单元格1-9只出现一次class Solution {public: bool isValidSudoku(vector<vector<char>>& board) { int i,j; char gc; for(i = 0; i <=8; ++i原创 2015-07-18 11:29:37 · 397 阅读 · 0 评论 -
[LeetCode]Count and Say
只用判断行、列、每个3*3的格子内是否满足1-9只出现一次的约束 BF方法class Solution {public: bool isValidSudoku(vectorvectorchar>>& board) { int i,j; char gc; for(i = 0; i 8; ++i) {原创 2015-07-15 15:38:07 · 314 阅读 · 0 评论 -
[LeetCode]Trapping Rain Water(!!!!)
DP的思想 关键是怎么来确定A[i]所能容纳的水, 会发现A[i]所能容纳的水由i左右两边最大值中较小的那个决定,所以,一个思路是对于每一个i找到其左边的最大值,和右边的最大值,都存下来,i能存的水是min(left,right)-A[i]class Solution {public: int trap(vector<int>& A) { if(A.size()<2)原创 2015-07-17 11:02:13 · 273 阅读 · 0 评论