前言
记录几个面试常见题型
一、知识点
递归,先列出所有情况,再处理
记忆化,可以减少重复列出的
递归注意终止条件
二、题目
1、https://leetcode-cn.com/problems/factorial-trailing-zeroes/submissions/
1.1 分析
阶乘求尾零,还有指数求
1.2代码
class Solution {
public:
int trailingZeroes(int n) {
if(n < 5)
{
return 0;
}
return n/5 + trailingZeroes(n / 5);
}
};
2、https://leetcode-cn.com/problems/number-of-steps-to-reduce-a-number-to-zero/
2.1题目分析
读懂题目
2.2代码
class Solution {
public:
int numberOfSteps(int num) {
if(num == 0)
{
return 0;
}
if(num % 2 == 1)
{
return numberOfSteps(num - 1) + 1;
}else{
return numberOfSteps(num / 2) + 1;
}
}
};
3https://leetcode-cn.com/problems/count-complete-tree-nodes/submissions/
3.1题目分析
先找总的情况
3.2代码
class Solution {
public:
int countNodes(TreeNode* root) {
if(root == nullptr) return 0;
return countNodes(root -> left) + 1 +countNodes(root -> right);
}
};
4https://leetcode-cn.com/problems/sZ59z6/
4.1题目分析
先根据二叉树性质遍历,然后寻找结果
4.2代码
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> a = vector<int>(1001,0);
void dg(TreeNode* root){
if(root){
a[root->val] = 1;
if(root->left){
dg(root->left);
}
if(root->right){
dg(root->right);
}
}
}
int numColor(TreeNode* root) {
dg(root);
int sum = 0;
for(int i = 0; i < a.size(); i++){
if(a[i]){
sum++;
}
}
return sum;
}
};
5https://leetcode-cn.com/problems/integer-replacement/submissions/
5.1题目分析
两种情况合并的一步,记忆搜索法,避免一些重复探索
5.2代码
class Solution {
private:
unordered_map<int, int> memo;
public:
int integerReplacement(int n) {
if (n == 1) {
return 0;
}
if (memo.count(n)) {
return memo[n];
}
if (n % 2 == 0) {
return memo[n] = 1 + integerReplacement(n / 2);
}
return memo[n] = 2 + min(integerReplacement(n / 2), integerReplacement(n/ 2 + 1));
}
};
6https://leetcode-cn.com/problems/range-sum-of-bst/submissions/
6.1题目分析
递归,深搜,分情况
6.2代码
class Solution {
public:
int rangeSumBST(TreeNode *root, int low, int high) {
if (root == nullptr) {
return 0;
}
if (root->val > high) {
return rangeSumBST(root->left, low, high);
}
if (root->val < low) {
return rangeSumBST(root->right, low, high);
}
return root->val + rangeSumBST(root->left, low, high) + rangeSumBST(root->right, low, high);
}
};
7https://leetcode-cn.com/problems/er-cha-shu-de-shen-du-lcof/submissions/
7.1题目分析
求深度和结点数有点类似
7.2代码
class Solution {
public:
int maxDepth(TreeNode* root) {
if(root == NULL)
return 0;
return max(maxDepth(root -> left) , maxDepth(root -> right)) + 1;
}
};
8 https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/
8.1题目分析
同上题
8.2代码
class Solution {
public:
int maxDepth(TreeNode* root) {
if(root == NULL)
return 0;
return max(maxDepth(root -> left) , maxDepth(root -> right)) + 1;
}
};
9https://leetcode-cn.com/problems/invert-binary-tree/submissions/
9.1题目分析
如何递下去重复一个情况, 找到终止条件
9.2代码
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if (root == nullptr) {
return nullptr;
}
TreeNode* left = invertTree(root->left);
TreeNode* right = invertTree(root->right);
root->left = right;
root->right = left;
return root;
}
};
10https://leetcode-cn.com/problems/bP4bmD/submissions/
10.1题目分析
dfs ,终止条件,初始条件
10.2代码
class Solution {
private:
void dfs(vector<vector<int>> &graph , vector<vector<int>>& ans, vector<int>& tmp , int i , int last)
{
if (i == last){
ans.push_back(tmp);
return;
}
for (auto n : graph[i]){
tmp.push_back(n);
dfs(graph, ans, tmp, n, last);
tmp.pop_back();
}
}
public:
vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
vector<vector<int>> ans;
vector<int> tmp;
tmp.push_back(0);
int lastNode = graph.size() - 1;
dfs(graph, ans, tmp, 0, lastNode);
return ans;
}
};
class Solution {
public:
int n;
vector<vector<int>> g;
vector<vector<int>> ans;
vector<int> path;
void dfs(int u) {
path.push_back(u);
if (u == n - 1) ans.push_back(path);
for (auto v: g[u]) dfs(v);
path.pop_back();
}
vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
g = graph;
n = g.size();
dfs(0);
return ans;
}
};