LeetCode
fu_ding1991
勿忘初心,方得始终
展开
-
LeetCode--Longest Substring Without Repeating Characters(字符串Hash)
题意:求出一字符串里面的最长不重复子串。 题解:设置一hash数组pos,代表子串中字符在字符串中位置,用tmpL代表遍历i个字符时以i字符为末尾的不重复子串长度。代码:class Solution { public: int lengthOfLongestSubstring(string s) { int maxL = 0; int tmpL = 0;原创 2015-03-15 19:24:13 · 320 阅读 · 0 评论 -
Shortest Palindrome(KMP)
题意:Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. Find and return the shortest palindrome you can find by performing this transformation.For exampl原创 2015-05-27 15:18:55 · 618 阅读 · 0 评论 -
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.题解:我们用栈来保存直方图中的递增子序列,若i在栈中,则在i之前比i小的直方图的下标原创 2015-05-28 11:45:19 · 402 阅读 · 0 评论 -
Interleaving String(动态规划求字符重构)
题意:Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.For example, Given: s1 = “aabcc”, s2 = “dbbca”,When s3 = “aadbbcbcac”, return true. When s3 = “aadbbbaccc”, return fa原创 2015-05-28 15:10:25 · 502 阅读 · 0 评论 -
Binary Tree Maximum Path Sum(树形动态规划)
题意:Given a binary tree, find the maximum path sum.The path may start and end at any node in the tree.For example: Given the below binary tree, 1 / \ 2 3 Return 6.题解:设dp[root]为从root一直往下走的最大和,设最大原创 2015-05-28 15:47:38 · 552 阅读 · 0 评论 -
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. 题解:类似于求最大子矩阵和,可以将i到j行压缩为1行,而一行的最长连续1就是我们要求的矩形的宽度。代码如下:class Solution { public:原创 2015-05-28 16:19:46 · 474 阅读 · 0 评论 -
LeetCode--Minimum Window Substring(最小匹配子串)
题意:Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).For example, S = “ADOBECODEBANC” T = “ABC” Minimum window is “BANC”.Not原创 2015-05-18 11:36:33 · 659 阅读 · 0 评论 -
LeetCode--Find Minimum in Rotated Sorted Array
题意:Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).Find the minimum element.You may assume no duplicate exists in the array.题解原创 2015-05-04 12:05:46 · 377 阅读 · 0 评论 -
Longest Substring Without Repeating Characters
题意: Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for “abcabcbb” is “abc”, which the length is 3.原创 2015-06-04 17:36:03 · 441 阅读 · 0 评论 -
Recover Binary Search Tree(BST中序遍历)
题意:Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure.题解:因为BST中序遍历(左中右)的结果肯定组成一个上升序列。 如:1,2,3,4,5,6. 所以若节点有交换,则肯定有逆序对的存在。 如:1,5,3,4,2原创 2015-05-27 16:21:46 · 725 阅读 · 0 评论 -
Palindrome Partitioning&II(回文字符串)
题意:找出一个字符串中的所有切割使得切割后的子串都是回文字符串。首先想到一个简单的DP判断从i到j是否能组成回文。 设dp[i][j] = 1代表a[i]到a[j]组成的子串是回文,反之则不是。 转移方程:若dp[i+1][j-1] = 1且a[i] = a[j]则dp[i][j] = 1。 然后寻找所有切割可以使用DFS,深搜每个可能的分割。代码如下:class Solution { pub原创 2015-05-26 11:54:11 · 370 阅读 · 0 评论 -
LeetCode--Binary Tree Postorder Traversal(栈实现三种遍历)
题意:Given a binary tree, return the postorder traversal of its nodes’ values.For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [3,2,1].Note: Recursive solution is triv原创 2015-03-30 11:19:23 · 312 阅读 · 0 评论 -
LeetCode--Unique Binary Search Trees II(DP求BST)
题意:Given n, generate all structurally unique BST’s (binary search trees) that store values 1…n. For example, Given n = 3, your program should return all 5 unique BST’s shown below.1 3 3原创 2015-03-25 15:27:23 · 581 阅读 · 0 评论 -
LeetCode--Two Sum(排序or Hash)
题意:从一堆数里面找出两个数,使它们的和满足target。排序代码;class Solution { public: class node { public: int num; int index; friend bool operator< (const node& x,const node& y) {原创 2015-03-15 18:34:25 · 362 阅读 · 0 评论 -
LeetCode--Add Two Numbers(链表)
双链表的加法。代码:/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: Lis原创 2015-03-15 19:58:47 · 329 阅读 · 0 评论 -
LeetCode--Merge k Sorted Lists
题意:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.题解:建立一个大小为k的最小堆,即堆顶存放最小元素,因为k个链表已排好序,借鉴归并排序的技巧,先取出堆顶的元素,再放入堆顶元素所在链表的表头的元素,再维持最小堆循环迭代。 注意直接链表存入会导致逆序原创 2015-03-30 10:05:02 · 297 阅读 · 0 评论 -
LeetCode--Reverse Nodes in k-Group
题意:Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is原创 2015-03-30 15:35:59 · 320 阅读 · 0 评论 -
LeetCode--Combination Sum(DFS)
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.The same repeated number may be chosen from C unlimited number of t原创 2015-03-18 18:20:00 · 322 阅读 · 0 评论 -
LeetCode--Convert Sorted Array to Binary Search Tree(平衡二叉搜索树)
题意:给定一有序数组,生成一颗平衡二叉树。 题解:平衡二叉树任意节点的左右子树深度相差最大为1. 因为二叉搜索树任意节点的左子树节点都比它小,右子树节点都比它大。所以对于一个有序数组我们选取中间的节点作为根,再分别递归地调用生成左右两个子树。代码如下:class Solution { public: TreeNode* buildTree(vector<int> &num,int lef原创 2015-04-01 09:50:53 · 365 阅读 · 0 评论 -
LeetCode--Sudoku Solver(DFS)
题意:求解9*9数独。 题解:典型的DFS,使用三个hash数组来判断行,列以及方块的取值情况,根据深搜不断更新数组。代码如下:class Solution { public: int solve(vector<vector<char> >& board,int r,int c){ if(r == 8&&c == 9) //数独完成原创 2015-04-01 11:39:21 · 356 阅读 · 0 评论 -
Best Time to Buy and Sell Stock IV(动态规划)
题意:有一支股票每天的价格波动,请在给定的时间内完成k次交易使收益最大,一次交易代表买入再卖出,交易不能重叠(必须卖完之后在买)。题解:我们首先设global[i][j]为到第i天位置交易j次的最大收益。 因为最后一次交易要么在第i天卖出,要么在之前卖出。在之前卖出的最大收益显然是global[i-1][j],而要求第i天卖出的最大收益我们需要枚举在前面哪一天买进。可以得到转移方程: globa原创 2015-05-27 10:44:06 · 457 阅读 · 0 评论