leetcode
云苓玉竹
目前是一名算法工程师
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
The minimum depth The maximum path sum path sum II
leetcode 上的四个问题 1 The minimum depth 2 The maximum depth 3 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 · 359 阅读 · 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 · 313 阅读 · 0 评论 -
convert sorted array to binary search tree
二叉树: balance Tree: 对于每个节点,其左右子树的高度差不超过1( BST tree: 对于 BST Tree,对于每个父节点,其左子节点小于父节点 其右子节点大于父节点 Convert Sorted Array to Binary Search Tree Given an array where elem原创 2016-03-28 16:19:28 · 352 阅读 · 0 评论 -
Convert Sorted List to Binary Search Tree
二叉树 本题将listnode转化为BST Convert Sorted List to Binary Search Tree Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.原创 2016-03-28 18:24:02 · 347 阅读 · 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 · 291 阅读 · 0 评论 -
interleaving string
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. Wh原创 2016-04-10 19:42:13 · 271 阅读 · 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 · 317 阅读 · 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 · 335 阅读 · 0 评论 -
single number III
对于本题的意思是: 给出一数组,数组中其中有两个元素不是重复的,剩余的元素都是两个重复的,返回这两个不重复的元素 这样说来,这题就有些特殊了,首先这个数组个数肯定是偶数,我们可以首先用sort排序,则第一个单独数肯定是位于基数位 上的,而且相同的数都是挨着的,可以用^ 来消除。具体程序如下: //author:liuyang //Time:28ms class Solution原创 2016-05-03 16:37:07 · 326 阅读 · 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 · 478 阅读 · 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 · 388 阅读 · 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 · 427 阅读 · 0 评论 -
Merge k Sorted Lists
/* 排序问题: 本题是将k个排好序的链表整合到一个链表中 具体的做法为: 首先进行两两合并,然后循环进行下去合并 Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. */ #include #i原创 2016-03-30 08:55:05 · 271 阅读 · 0 评论 -
sort colors
Sort Colors Given 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 · 325 阅读 · 0 评论 -
Subsets
/* 暴力枚举 subsets: 给出一组数组,列出所有可能的子集 对于n个元素,有2^n个子集 对于此题构造一个bool类型的向量select 每个元素可以选也可以不选 */ #include #include #include using namespace std; class Solution { public: vector> Subset(vector &Arr原创 2016-04-01 01:24:42 · 276 阅读 · 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 #include using原创 2016-04-01 06:00:38 · 312 阅读 · 0 评论 -
subsets
subsets: 给出一组数组,列出所有可能的子集 对于n个元素,有2^n个子集 对于此题构造一个bool类型的向量select 每个元素可以选也可以不选 Subsets II Given a collection of integers that might contain duplicates, S, retu原创 2016-04-01 06:01:56 · 303 阅读 · 0 评论 -
palindrome
/* DFS palindrome partation input ['"aab"] output ["a","aa","b"] */ #include #include #include using namespace std; class Solution { public: vector> Subset(string s) { vector>result; v原创 2016-04-04 15:31:04 · 261 阅读 · 0 评论 -
N_queen
对于n(n>=4)皇后问题: 任意两个Queens不能同行同列,而且不能共对角线(主对角,副对角) 其中主对角行列相减的绝对值是相等的 副对角线行列相加是相等的 本题对于n*n格的棋盘中有多少种满足上述条件的解: 本题首先设置全局的计数count, 然后设置相应的剪枝项 colmun main_diag anti_diag 然后使用深度优先搜索法: /* 输出N皇后问题的解数原创 2016-04-05 10:27:27 · 652 阅读 · 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 · 357 阅读 · 0 评论
分享