LeetCode Unique Binary Search Trees
摘要:链接: https://oj.leetcode.com/problems/unique-binary-search-trees/dp[i]表示当n为i时的BST数量..递推关系如下图这样,就把n=4分割为n=3,n=1,n=2.用dp数组记录结果代码:class Solution{ public: ...
阅读全文
LeetCode Best Time to Buy and Sell Stock
摘要:链接: https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock/开始理解错题意了..只能买一次&&卖一次.找出最大利润看了网上的思路...O(n)的复杂度..ACclass Solution{ public: int max...
阅读全文
LeetCode Partition List
摘要:链接: https://oj.leetcode.com/problems/partition-list/双重指针....../** * Definition for singly-linked list. * struct ListNode { * int val; * ListNo...
阅读全文
LeetCode Path Sum
摘要:链接: https://oj.leetcode.com/problems/path-sum/深度优先搜索,.求出个从根到叶子节点的和..注意树中有负树/** * Definition for binary tree * struct TreeNode { * int val; * T...
阅读全文
LeetCode Maximum Subarray
摘要:链接: https://oj.leetcode.com/problems/maximum-subarray/最大连续子序列 动态规划class Solution{ public: int maxSubArray(int A[],int n) { int dp[n+1]; int ans=...
阅读全文
LeetCode Two Sum
摘要:链接: https://oj.leetcode.com/problems/two-sum/在一个数组中找出两个值,使得他们的和等于 target; 返回相应的下标+1依次查找是存在值等于 target-numbers[i]class Solution{ public: vector twoSum(...
阅读全文
LeetCode Find Minimum in Rotated Sorted Array
摘要:链接: https://oj.leetcode.com/problems/find-minimum-in-rotated-sorted-array/旋转数组 查找最小值 最小值肯定在旋转轴处 ,所以 用二分搜索找出旋转轴就可以了每次取中点,前后肯定有一截数是有序,另一截是无序..取无序的那部分继续搜...
阅读全文
LeetCode Add Binary
摘要:链接: https://oj.leetcode.com/problems/add-binary/大数加法.二进制class Solution{ public: string addBinary(string a,string b) { bool sign=false; string an...
阅读全文
LeetCode Gray Code
摘要:链接: https://oj.leetcode.com/problems/gray-code/格雷码 见维基百科 http://zh.wikipedia.org/wiki/%E6%A0%BC%E9%9B%B7%E7%A0%81知道原理后就很简单了class Solution{ public: ve...
阅读全文
POJ 1273 Drainage Ditches
摘要:链接: http://poj.org/problem?id=1273最大流问题.#include #include #include #define MAX_M 200#define INF 100000000using namespace std;typedef struct{ int to; i...
阅读全文
LeetCode Word Break
摘要:链接: https://oj.leetcode.com/problems/word-break/dp[i] 表示s[0~i] 能否分割成dict中的单词class Solution{ public: bool wordBreak(string s,unordered_set &dict) { ...
阅读全文
LeetCode Set Matrix Zeroes
摘要:链接: https://oj.leetcode.com/problems/set-matrix-zeroes/把矩阵中的零全部投影到第一行和第一列..第一行和第一列中的零要单独考虑class Solution{ public: void setZeroes(vector > &matrix) ...
阅读全文
LeetCode Convert Sorted Array to Binary Search Tree
摘要:链接: https://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/把一个有序数组转换成一棵AVL树/** * Definition for binary tree * struct TreeNode { *...
阅读全文
LeetCode Gas Station
摘要:链接: https://oj.leetcode.com/problems/gas-station/网上的思路:如果 从i不能到 j 则从i~j之间的任何一个出发都不能到j如果 总的油量小与总的消耗量,则无论从哪出发,都不能到达class Solution{ public: int canCompl...
阅读全文
LeetCode Palindrome Partitioning
摘要:链接: https://oj.leetcode.com/problems/palindrome-partitioning/dp+dfs求字符串s的所有分割,使得每一个子串都是回文字符串.dp数组用来储存s[i]~s[j]是否为回文串.由于二维数组传递不方便,所以用dp[i]来计算 :dp[i][j]...
阅读全文