---恢复内容开始---

2018.3.16目前已刷27题,打卡记录有意思的题目。

leetcode78 subsets 思路1:DFS遍历子集,每遇到一个数就把该数加上原来的子集变成新的子集。

class Solution {
private:
void DFS(int index,vector<int> t,vector<int>& nums,vector<vector<int>> & res){
res.push_back(t);
for(int i = index;i<nums.size();i++){
t.push_back(nums[i]);
DFS(i+,t,nums,res);
t.pop_back();
}
} public:
vector<vector<int>> subsets(vector<int>& nums) {
vector<vector<int>> res;
vector<int> t;
DFS(,t,nums,res);
return res;
}
};

思路2:采用位运算,是否取某个数组成子集视为0101的序列。

236  求二叉树的最低公共祖先

思路1:求出根节点到所求节点的路径,最后求两个路径的最后一个公共节点

class Solution {
//pNode是要打印的节点,pRoot是工作指针
bool getNodePath(TreeNode* pRoot,TreeNode * pNode,vector<TreeNode*> &path){
if(!pRoot) return false;
path.push_back(pRoot);
if(pRoot==pNode) return true;
bool found=false;
if(pRoot->left) found=getNodePath(pRoot->left,pNode,path);
if(!found&&pRoot->right) found=getNodePath(pRoot->right,pNode,path);
if(!found) path.pop_back();
return found; }
TreeNode* getLastCommonNode(const vector<TreeNode*> &pPath,const vector<TreeNode*> &qPath){
TreeNode* res;
vector<TreeNode*>::const_iterator iteratorP = pPath.begin();
vector<TreeNode*>::const_iterator iteratorQ = qPath.begin();
while(iteratorP != pPath.end() && iteratorQ != qPath.end()){
if(*iteratorP == *iteratorQ) res=*iteratorP;
iteratorP++;
iteratorQ++;
}
return res;
}
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
vector<TreeNode*> pPath,qPath;
if(!root || !p || !q) return nullptr;
getNodePath(root,p,pPath);
getNodePath(root,q,qPath);
return getLastCommonNode(pPath,qPath);
}
};

思路2:先序遍历

 class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if (root == NULL || root == p || root == q) return root;
//如果没有匹配到节点 就会返回null
//但凡匹配到任何一个节点,都不会返回null;
TreeNode* left = lowestCommonAncestor(root->left, p, q);
TreeNode* right = lowestCommonAncestor(root->right, p, q);
//如果左边,右边都找到了节点,那就是最近的公共祖先
if (left && right)
return root;
return left == NULL ? right : left;
}
};

leetcode690,题目虽然简单,但是因为某个变量没有初始化为0,导致相同的测试用例和代码在本地执行与提交代码跑出来结果不同。这个低级错误真是让人脸红啊~~

leetcode300 最长不上升子序列

方法一:动态规划算法。状态转移方程   dp[i]=max{1,dp[j]+1}  (j=1,2,...,i-1 && A[j]<A[i])

class Solution {
public:
int lengthOfLIS(vector<int>& nums) {
if(nums.empty()) return ;
int maxLen=;
vector<int> dp= vector<int>(nums.size(), );
for(int i=;i<nums.size();i++){
for(int j=;j<i;j++){
if(nums[i]>nums[j] && dp[i]<dp[j]+)
dp[i]=dp[j]+;
}
maxLen=dp[i]>maxLen?dp[i]:maxLen;
}
return maxLen;
}
};

方法二:

leetcode 78,236,300的更多相关文章

  1. LeetCode 78,面试常用小技巧,通过二进制获得所有子集

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是LeetCode专题第47篇文章,我们一起来看下LeetCode的第78题Subsets(子集). 这题的官方难度是Medium,点赞 ...

  2. [Leetcode 78]求子集 Subset

    [题目] Given a set of distinct integers, nums, return all possible subsets (the power set). Note: The ...

  3. Leetcode 78题-子集

    LeetCode 78 网上已经又很多解这题的博客了,在这只是我自己的解题思路和自己的代码: 先贴上原题: 我的思路: 我做题的喜欢在本子或别处做写几个示例,以此来总结规律:下图就是我从空数组到数组长 ...

  4. 在vscode中配置LeetCode插件,从此愉快地刷题

    大家好,今早在B站看到up主的vscode里藏了leetcode插件,这才知道原来还有这款神器.但是没想到在用的时候遇到了一些麻烦,花了一点时间才解决.所以写这篇文章除了给大家安利这个好用的插件之外, ...

  5. LeetCode 81,在不满足二分的数组内使用二分法 II

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是LeetCode专题第50篇文章,我们来聊聊LeetCode中的81题Search in Rotated Sorted ArrayII ...

  6. LeetCode 二叉树,两个子节点的最近的公共父节点

    LeetCode 二叉树,两个子节点的最近的公共父节点 二叉树 Lowest Common Ancestor of a Binary Tree 二叉树的最近公共父亲节点 https://leetcod ...

  7. [LeetCode] 78. Subsets 子集合

    Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...

  8. LeetCode 78. 子集(Subsets) 34

    78. 子集 78. Subsets 题目描述 给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明: 解集不能包含重复的子集. 每日一算法2019/6/6Day 34L ...

  9. [转]通过Mesos、Docker和Go,使用300行代码创建一个分布式系统

    http://www.csdn.net/article/2015-07-31/2825348 [编者按]时下,对于大部分IT玩家来说,Docker和Mesos都是熟悉和陌生的:熟悉在于这两个词无疑已成 ...

随机推荐

  1. python locust 性能测试:HttpSession

    官网解释:用于在请求之间执行Web请求和保留(会话)cookie的类(以便能够登录和退出网站):记录每个请求,以便locust可以显示统计信息: from locust import TaskSet, ...

  2. Nginx 出现 _STORAGE_WRITE_ERROR_:./Runtime/Cache/Home/

    Nginx 出现  _STORAGE_WRITE_ERROR_:./Runtime/Cache/Home/ 这种情况是因为 application 没有足的权限 .需要给予777的权限就能解决了

  3. Java第一次实训课

    //1.1 声明一个整型变量a,并赋初值5,在程序中判断a是奇数还是偶数,然后输出判断的结果. package mingye; public class Exc { public static voi ...

  4. 原 HTML5+规范:barcode(条码扫描)

    https://blog.csdn.net/qq_27626333/article/details/51815121 引用,版权归作者所有:

  5. Linux开局配置注意事项

    1.修改ssh配置文件远程端口号,防止攻击 sed    -ri   's/“#Port 22”/“Port 10086”/g‘    /etc/ssh/sshd_config 2.修改ssh配置文件 ...

  6. 2.2JAVA基础复习——JAVA语言的基础组成运算符和语句

    JAVA语言的基础组成有: 1.关键字:被赋予特殊含义的单词. 2.标识符:用来标识的符号. 3.注释:用来注释说明程序的文字. 4.常量和变量:内存存储区域的表示. 5.运算符:程序中用来运算的符号 ...

  7. JZ2440学习笔记之第一个裸机程序(Keil-MDK)

    CPU:S3C2440, ARM920T, Internal 4KB RAM, Support boot from NAND flash, 128MB for each bank. JZ2440:Me ...

  8. opencv学习之路(41)、人脸识别

    一.人脸检测并采集个人图像 //take_photo.cpp #include<opencv2/opencv.hpp> using namespace cv; using namespac ...

  9. Spring boot Spring cloud 框架搭建

    随笔记载几个框架搭建时的坑: 这个是server提供者模块,需要注意的是spring:application:name 接下来是fegin模块,需要主要注意信息已说明,需要特别说明的是RequestM ...

  10. freeswitch 事件命令

    1.uuid_bridge 桥接两条呼叫的腿. Usage: uuid_bridge <uuid> <other_uuid> uuid_bridge至少需要有一条腿是被呼通的. ...