随笔分类 - Leetcode
leetcode刷题记录
摘要:思路 1、判断左子树是否为空,若为空则直接往右走,若不为空则22、将当前节点root的右子树接到当前root节点的左孩子节点的最右下边的孩子节点3、将当前节点root的左子树接到右子树上,并将左节点置为NULL。 /** * Definition for a binary tree node. *
阅读全文
摘要:思路 代码: class Solution { public: vector<int> countBits(int num) { vector<int>result(num+1); result[0] = 0; for(int i =1;i<=num;i++) { if(i%2 == 0){ res
阅读全文
摘要:思路: 先对数组进行排序,然后遍历寻找连续序列并计数,注意相等的情况 代码 class Solution { public: int longestConsecutive(vector<int>& nums) { if(nums.empty()) return 0; sort(nums.begin(
阅读全文
摘要:思路:回溯 递归 代码 class Solution { public: void back_track_Dfs(vector<string>&res,string path,int n,int lc,int rc) { if(rc>lc||rc>n||lc>n)//不满足条件 { return;
阅读全文
摘要:# 思路深度优先遍历遍历整个数组,遇到1,ans++,ans是记录岛的个数的运行一下dfs函数,把这个岛所有陆地给我沉喽,这个岛全部的1变成0等把grid全遍历完,grid就全是0了,再把ans输出,这个ans就是我们记录的岛的个数注意:grid竟然是char类型的,所有1和0都要加单引号哦 代码
阅读全文
摘要:思路 动态规划 斐波拉契呀数列 代码 class Solution { public: int climbStairs(int n) { vector<int>res_dp(n+5,0); res_dp[1] = 1; res_dp[2] = 2; for(int i = 3;i<=n;i++) {
阅读全文
摘要:思路 深度优先遍历:回溯,递归(终止条件:选择的当前数字数量等于原始数组的大小) 状态变量1.递归到第几层,depth2.已经选择了哪些数Path3.布尔数组 used非叶子节点选择分枝 代码: class Solution { public: void backtrack(vector<vecto
阅读全文
摘要:方法1:递归思路 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left
阅读全文
摘要:方法1 递归解法: 左右根 代码 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(
阅读全文
摘要:前序遍历迭代算法: 二叉树的前序遍历二叉树的遍历,整体上看都是好理解的。三种遍历的迭代写法中,数前序遍历最容易理解。递归思路:先树根,然后左子树,然后右子树。每棵子树递归。 代码: /** * Definition for a binary tree node. * struct TreeNode
阅读全文
摘要:方法一:内置位计数功能 思路 大多数编程语言中,都存在各种内置计算等于 1 的位数函数。如果这是一个项目中的问题,应该直接使用内置函数,而不是重复造轮子。但这是一个力扣问题,有人会认为使用内置函数就好像使用 使用 LinkedList 实现 LinkedList。对此,我们完全赞同。因此后面会有手工
阅读全文
摘要:相关概念 先序遍历:根节点,左节点,右节点中序遍历:左节点,根节点,右节点 所以构建二叉树的问题本质上就是: 找到各个子树的根节点 root构建该根节点的左子树构建该根节点的右子树 递归思路: 代码: # Definition for a binary tree node. # class Tree
阅读全文
摘要:思路1 卡塔兰数 代码 class Solution { public: int numTrees(int n) { long C = 1; for (int i = 0;i<n;++i) { C = C*2*(2*i+1)/(i+2); } return int(C); } };
阅读全文
摘要:思路: l1 或 l2若有一者为空则返回非空链表若都非空,则判断 l1 和 l2 的val,val小的将其 next 递归添加到结果的节点递归终止条件:l1 或 l2 有一为空 代码: /** * Definition for singly-linked list. * struct ListNod
阅读全文
摘要:思路:递归求解某一棵树的左右节点,返回 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : v
阅读全文
摘要:思路 递归 用一个函数辅助判断左右子树是否完全对称,对根节点进行输入递归判断结果。 # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # sel
阅读全文
摘要:#思路 # 思路:定义一个当前节点,赋值为head,定义一个pre作为反转后的第一个节点,定义一个临时node 存放当前节点的下一个节点 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNod
阅读全文
摘要:思路:递归得到左右子树,交换左右子树,返回根节点 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x
阅读全文
摘要:/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), r
阅读全文
摘要:股票问题的方法就是 动态规划,因为它包含了重叠子问题,即买卖股票的最佳时机是由之前买或不买的状态决定的,而之前买或不买又由更早的状态决定的... 思路(官方题解方法二:一次遍历) 遍历一遍数组,计算每次 到当天为止 的最小股票价格和最大利润。定义一个变量保存最大利润,同时定义一个变量保存最小的股票价
阅读全文

浙公网安备 33010602011771号