![](https://img-blog.csdnimg.cn/20201014180756916.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
leetcode
云苓玉竹
目前是一名算法工程师
展开
-
The minimum depth The maximum path sum path sum II
leetcode 上的四个问题1 The minimum depth 2 The maximum depth3 path sum 4 path sum II本文一次性放到一个函数中运行:其定义为:The minimum depth is the number of nodes along the shortest path from the root原创 2016-03-29 16:01:41 · 301 阅读 · 0 评论 -
validate binary search tree
二叉树 leetcode:本题是关于给出的二叉树是否为BST树:BST定义: 对于任意一个parent节点,其leftchild node 小于 parent rightchild node 大于 parent节点程序采用递归遍历:#include #include using namespace std;struct TreeNode{原创 2016-03-28 15:22:00 · 269 阅读 · 0 评论 -
convert sorted array to binary search tree
二叉树:balance Tree:对于每个节点,其左右子树的高度差不超过1(BST tree:对于 BST Tree,对于每个父节点,其左子节点小于父节点其右子节点大于父节点Convert Sorted Array to Binary Search TreeGiven an array where elem原创 2016-03-28 16:19:28 · 301 阅读 · 0 评论 -
Convert Sorted List to Binary Search Tree
二叉树本题将listnode转化为BSTConvert Sorted List to Binary Search TreeGiven a singly linked list where elements aresorted in ascending order, convert it to a height balanced BST.原创 2016-03-28 18:24:02 · 294 阅读 · 0 评论 -
best time to buy and sell stocks III
对于这题,题目要求应该是最多交易两次对于同一天内买进又卖出相当与没有参与交易所以对于给出的一列数组【a1,a2,a3,...ai.....an】可以分为两段在ai处,【a1....ai】的总利润+【a(i+1),...an】的利润=利润将数组变成差分数组,分别求两段对应的利润 class Solution {public: in原创 2016-04-10 10:37:26 · 245 阅读 · 0 评论 -
interleaving string
Interleaving StringGiven 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.Wh原创 2016-04-10 19:42:13 · 224 阅读 · 0 评论 -
edit distance
//动态规划//f[i][j]表示字符串a从1到i的子串和字符串b从1到j的字串的编辑距离(下标从1开始)//f[i-1][j]+1表示delete操作//f[i][j-1]+1表示insert操作//f[i-1][j-1]+f1(x,y)其中f1(x,y) x=y时f1(x,y)=0//else f1(x,y)=1 表示替换操作class Solution {public:原创 2016-04-10 20:38:39 · 269 阅读 · 0 评论 -
distinct subsequences
//动态规划//题意是用删除字符的方法,字符串s1变成字符串s2,一共有多少种变换方法//f[i][j]表示有字符串i变成字符串j的总变换方法//当s1[i]=s2[j]时,f[i][j]=f[i][j]+f[i-1][j]意思是当//s1[i]=s2[j]时,当前的字母可以保留也可以抛弃//所以f[i][j]等于保留此字母使得变换方法加上不保此字母的变换方法//当s1[i]!=s2[原创 2016-04-10 21:43:38 · 263 阅读 · 0 评论 -
single number III
对于本题的意思是: 给出一数组,数组中其中有两个元素不是重复的,剩余的元素都是两个重复的,返回这两个不重复的元素这样说来,这题就有些特殊了,首先这个数组个数肯定是偶数,我们可以首先用sort排序,则第一个单独数肯定是位于基数位上的,而且相同的数都是挨着的,可以用^ 来消除。具体程序如下://author:liuyang//Time:28msclass Solution原创 2016-05-03 16:37:07 · 259 阅读 · 0 评论 -
Unique Binary Tree
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 3 2 1原创 2016-03-28 11:31:11 · 423 阅读 · 0 评论 -
jump game I // II
/*贪心算法jump game 给出一列数组,非负,数组每个元素表示可以跳跃的最大步数第一个元素是初始化位置,编写一个函数判断是否成立(跳出数组)分析:对于数组A,共有n层,每次最多可以跳A[i]步,我们可以这样分析,加入从第0层开始,一层一层的往上跳,每次可以跳1步,那么对于跳到第i层时,距离0层的最远距离就是i-1+1+A[i-1]此时我们需要判断的就是这个最原创 2016-04-08 16:30:33 · 329 阅读 · 0 评论 -
Binary Tree Maximum Path Sum
/*二叉树:本题是为了解决对于任意一个Binary Tree ,我们要求出其最大路径和,即是哪条路径节点值得和最大,将和返回即可。本题难点在于对于BT,他可以从任意node 开始,也可以在any node stop.因此,我们可以采用深度优先搜索法(DFS)我们算出左右子树的数值l,r,如果l>0,表明左子树对整个结果有利,可以加上,同理如果r>0,表明右子树也有利,原创 2016-03-29 17:30:02 · 357 阅读 · 0 评论 -
Merge k Sorted Lists
/*排序问题:本题是将k个排好序的链表整合到一个链表中具体的做法为:首先进行两两合并,然后循环进行下去合并Merge k Sorted ListsMerge k sorted linked lists and return it as one sorted list.Analyze and describe its complexity.*/#include#i原创 2016-03-30 08:55:05 · 232 阅读 · 0 评论 -
sort colors
Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.Here, we w原创 2016-03-30 11:57:47 · 284 阅读 · 0 评论 -
Subsets
/*暴力枚举subsets:给出一组数组,列出所有可能的子集对于n个元素,有2^n个子集对于此题构造一个bool类型的向量select每个元素可以选也可以不选*/#include #include #includeusing namespace std;class Solution{public: vector> Subset(vector &Arr原创 2016-04-01 01:24:42 · 229 阅读 · 0 评论 -
permutations
/*暴力枚举permutations 排列[1,2,3]permutations:[1,2,3] [1,3,2][2,1,3] [2,3,1][3,1,2] [3,2,1]分析:使用深度搜索:从左到右遍历,变量i看是否在已选路径中如果在,push_back(),继续若果不再,返回上一层*/#include #include#includeusing原创 2016-04-01 06:00:38 · 254 阅读 · 0 评论 -
subsets
subsets:给出一组数组,列出所有可能的子集对于n个元素,有2^n个子集对于此题构造一个bool类型的向量select每个元素可以选也可以不选Subsets IIGiven a collection of integers that might contain duplicates, S, retu原创 2016-04-01 06:01:56 · 247 阅读 · 0 评论 -
palindrome
/*DFSpalindrome partationinput ['"aab"]output ["a","aa","b"]*/#include #include#includeusing namespace std;class Solution{public: vector> Subset(string s) { vector>result; v原创 2016-04-04 15:31:04 · 209 阅读 · 0 评论 -
N_queen
对于n(n>=4)皇后问题:任意两个Queens不能同行同列,而且不能共对角线(主对角,副对角)其中主对角行列相减的绝对值是相等的副对角线行列相加是相等的本题对于n*n格的棋盘中有多少种满足上述条件的解:本题首先设置全局的计数count,然后设置相应的剪枝项colmunmain_diaganti_diag然后使用深度优先搜索法:/*输出N皇后问题的解数原创 2016-04-05 10:27:27 · 589 阅读 · 0 评论 -
Merge K Sorted Lists
对于本题: 要将k个链表整合到一个链表中,首先进入脑海的想法是两两Merge,然后循环进行,然后整合到一个链表p中;具体程序如下://runtime:312ms/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * L原创 2016-05-03 17:53:10 · 302 阅读 · 0 评论