上一页 1 2 3 4 5 6 7 8 9 10 ··· 23 下一页
方法一:内置位计数功能 思路 大多数编程语言中,都存在各种内置计算等于 1 的位数函数。如果这是一个项目中的问题,应该直接使用内置函数,而不是重复造轮子。但这是一个力扣问题,有人会认为使用内置函数就好像使用 使用 LinkedList 实现 LinkedList。对此,我们完全赞同。因此后面会有手工 Read More
posted @ 2021-09-26 15:47 A-inspire Views(35) Comments(0) Diggs(0)
相关概念 先序遍历:根节点,左节点,右节点中序遍历:左节点,根节点,右节点 所以构建二叉树的问题本质上就是: 找到各个子树的根节点 root构建该根节点的左子树构建该根节点的右子树 递归思路: 代码: # Definition for a binary tree node. # class Tree Read More
posted @ 2021-09-26 15:43 A-inspire Views(35) Comments(0) Diggs(0)
思路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); } }; Read More
posted @ 2021-09-26 15:41 A-inspire Views(25) Comments(0) Diggs(0)
思路: l1 或 l2若有一者为空则返回非空链表若都非空,则判断 l1 和 l2 的val,val小的将其 next 递归添加到结果的节点递归终止条件:l1 或 l2 有一为空 代码: /** * Definition for singly-linked list. * struct ListNod Read More
posted @ 2021-09-26 15:39 A-inspire Views(24) Comments(0) Diggs(0)
思路:递归求解某一棵树的左右节点,返回 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : v Read More
posted @ 2021-09-26 15:37 A-inspire Views(20) Comments(0) Diggs(0)
思路 递归 用一个函数辅助判断左右子树是否完全对称,对根节点进行输入递归判断结果。 # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # sel Read More
posted @ 2021-09-26 15:36 A-inspire Views(31) Comments(0) Diggs(0)
#思路 # 思路:定义一个当前节点,赋值为head,定义一个pre作为反转后的第一个节点,定义一个临时node 存放当前节点的下一个节点 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNod Read More
posted @ 2021-09-26 15:33 A-inspire Views(27) Comments(0) Diggs(0)
思路:递归得到左右子树,交换左右子树,返回根节点 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x Read More
posted @ 2021-09-26 15:26 A-inspire Views(29) Comments(0) Diggs(0)
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), r Read More
posted @ 2021-09-26 15:25 A-inspire Views(29) Comments(0) Diggs(0)
股票问题的方法就是 动态规划,因为它包含了重叠子问题,即买卖股票的最佳时机是由之前买或不买的状态决定的,而之前买或不买又由更早的状态决定的... 思路(官方题解方法二:一次遍历) 遍历一遍数组,计算每次 到当天为止 的最小股票价格和最大利润。定义一个变量保存最大利润,同时定义一个变量保存最小的股票价 Read More
posted @ 2021-09-26 15:24 A-inspire Views(22) Comments(0) Diggs(0)
上一页 1 2 3 4 5 6 7 8 9 10 ··· 23 下一页