LeetCode
xxzccccccc
python/C++, 深度学习,ACM算法竞赛
阿里算法工程师
前百度算法工程师
前滴滴算法工程师
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
Leetcode 1579. 保证图可完全遍历
解法:并查集+路径压缩 设置并查集 father[] ,father_2 对type = 3进行并查集操作,将边一条一条压入并查集中,然后对于在一个father中的边,不加入并查集,并统计数量为ans,即type=3可以删除的边。 father_2 保存一下father的结果,一会给type=2用 根据type = 1获得 father[] ,父亲儿子的信息(即并查集的信息),继续对type=1的边做并查集操作,如果两条边在一个并查集,则不加入,并统计数量为ans1, 同理对于father_2 ,原创 2020-09-08 18:41:56 · 402 阅读 · 0 评论 -
Leetcode 124. 二叉树中的最大路径和
题目内容: 给定一个非空二叉树,返回其最大路径和。 本题中,路径被定义为一条从树中任意节点出发,沿父节点-子节点连接,达到任意节点的序列。该路径至少包含一个节点,且不一定经过根节点。 示例 1: 输入:[1,2,3] 1 / \ 2 3 输出:6 示例2: 输入:[-10,9,20,null,null,15,7] -10 / \ 9 20 / \ 15 7 输出:42 如下代码1:AC代码 根据题意,...原创 2020-09-05 12:48:20 · 328 阅读 · 0 评论 -
LeetCode 剑指 Offer 34. 二叉树中和为某一值的路径
输入一棵二叉树和一个整数,打印出二叉树中节点值的和为输入整数的所有路径。从树的根节点开始往下一直到叶节点所经过的节点形成一条路径。 示例: 给定如下二叉树,以及目标和sum = 22, 5 / \ 4 8 / / \ 11 13 4 / \ / \ 7 2 5 1 返回: [ [5,4,11,2], ...原创 2020-09-02 11:03:21 · 290 阅读 · 0 评论 -
Leetcode 990. 等式方程的可满足性
给定一个由表示变量之间关系的字符串方程组成的数组,每个字符串方程 equations[i] 的长度为 4,并采用两种不同的形式之一:"a==b" 或"a!=b"。在这里,a 和 b 是小写字母(不一定不同),表示单字母变量名。 只有当可以将整数分配给变量名,以便满足所有给定的方程时才返回true,否则返回 false。 题目 示例 1: 输入:["a==b","b!=a"] 输出:false 解释:如果我们指定,a = 1 且 b = 1,那么可以满足第一个方程,但无法满足第二个方程。没有办法...原创 2020-09-01 22:32:29 · 240 阅读 · 0 评论 -
LeetCode 剑指 Offer 20. 表示数值的字符串
题目: 请实现一个函数用来判断字符串是否表示数值(包括整数和小数)。例如,字符串"+100"、"5e2"、"-123"、"3.1416"、"-1E-16"、"0123"都表示数值,但"12e"、"1a3.14"、"1.2.3"、"+-5"及"12e+5.4"都不是。 思路:一开始想整个字符处理,但是发现容易出现判断条件过多的情况。故根据科学计数法的形式将数值分成两个(底数和浮点数两块)。即E前面的字符temp1,和E后面的字符temp2。 前面和后面的两个数判断标准基本一样,唯一差别为浮点数tem.原创 2020-09-01 15:44:10 · 282 阅读 · 0 评论 -
206. Reverse Linked List C++、Python
题目链接:206. Reverse Linked List C++ class Solution{ public: ListNode* reverseList(ListNode* head){ stack<ListNode*> s; ListNode* temp =head; while(temp!=NULL){ ...原创 2020-02-02 19:36:50 · 167 阅读 · 0 评论 -
448. Find All Numbers Disappeared in an Array C++、Python
题目链接:448. Find All Numbers Disappeared in an Array C++ class Solution { public: vector<int> findDisappearedNumbers(vector<int>& nums) { int len = nums.size(); ve...原创 2020-02-02 19:07:15 · 250 阅读 · 0 评论 -
155. Min Stack C++、Python
题目链接:155. Min Stack C++ class MinStack{ public: stack<int> num,min1; MinStack(){} void push(int x){ num.push(x); if(min1.empty())min1.push(x); else if(x<...原创 2020-02-02 17:16:52 · 180 阅读 · 0 评论 -
169. Majority Element C++、Python
题目链接:169. Majority Element C++ class Solution{ public: int majorityElement(vector<int>& nums){ sort(nums.begin(),nums.end()); return nums[nums.size()/2]; } }; Pyt...原创 2020-02-02 16:54:37 · 142 阅读 · 0 评论 -
121. Best Time to Buy and Sell Stock C++、Python
题目链接:121. Best Time to Buy and Sell Stock C++ class Solution { public: int maxProfit(vector<int>& prices) { if(!prices.size())return 0; vector<int> maxt; ...原创 2020-02-02 12:12:08 · 196 阅读 · 0 评论 -
104. Maximum Depth of Binary Tree
题目链接:104. Maximum Depth of Binary Tree C++ 1.dfs class Solution{ private: int ans=0; void def(TreeNode*root ,int depth){ if(root->left==NULL && root->left==NULL) ans=...原创 2020-02-01 20:37:59 · 157 阅读 · 0 评论 -
283. Move Zeroes C++、Python
题目链接:283. Move Zeroes C++ class Solution{ public: void moveZeroes(vector<int>& nums){ int cnt =0; for(int i=0;i<nums.size();i++){ if(nums[i]!=0)swap(num...原创 2020-02-01 14:55:40 · 156 阅读 · 0 评论 -
70. Climbing Stairs C++、Python
题目链接:70. Climbing Stairs C++ //Approach 1 //动态规划问题,假设 dp[i] 表示登上阶梯i的方法数,则dp[i] = dp[i-1] + dp[i-2] 其中 i >=2 dp[0]=0 dp[1]=1 dp[2]=2 class Solution{ public: int climbStairs(int n){ ...原创 2020-02-01 13:30:49 · 214 阅读 · 0 评论 -
53. Maximum Subarray C++、Python
题目链接:53. Maximum Subarray C++ //Approach 1 //情况一、对于只有负数的数组而言,结果为所有负数中最大的数 //情况二、对于有正数情况下,从做往右边扫,维护一个和为正的区间,如果为负数则放弃该区间(详细见代码) class Solution{ public: int maxSubArray(vector<int>& n...原创 2020-01-31 20:40:32 · 208 阅读 · 0 评论 -
21. Merge Two Sorted Lists C++、Python
题目链接:21. Merge Two Sorted Lists C++ class Solution{ public: ListNode* mergeTwoLists(ListNode* l1,ListNode* l2){ ListNode* ans = new ListNode(0); ListNode* cur = ans; wh...原创 2020-01-31 17:45:30 · 152 阅读 · 0 评论 -
2. Add Two Numbers C++、Python
题目链接:2. Add Two Numbers 栈实现数字加法 C++ class Solution{ public: ListNode* addTwoNumbers(ListNode* l1,ListNode* l2){ int sum=0; ListNode* dummpy =new ListNode(0), *cur = dummpy; ...原创 2020-01-30 21:46:13 · 278 阅读 · 0 评论 -
20. Valid Parentheses C++、python
题目链接:20. Valid Parentheses 直接用栈实现 //C++ class Solution{ public: bool isValid(string s){ stack<char> q; for(int i=0;i<s.size();i++){ if(s[i]=='('||s[i]=='{'|...原创 2020-01-30 19:54:36 · 156 阅读 · 0 评论 -
617. Merge Two Binary Trees 、136. Single Number(C++、Python)
617.题目链接:Merge Two Binary Trees C++ //C++ //Approach #1 Using Recursion //时间复杂度O(n),空间复杂度O(n) class Solution { public: TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) { if(t1==nullptr...原创 2020-01-30 15:04:26 · 279 阅读 · 0 评论 -
LeetCode Two Sum ——C++、Python
题目链接:Two Sum C++ //C++ //Approach 1:Brute Force //时间复杂度O(n^2),空间复杂度O(1) class Solution{ public: vector<int> twoSum(vector<int>& nums, int target){ vector<int> ret;...原创 2020-01-30 13:25:47 · 204 阅读 · 0 评论
分享