本文章的所有题目信息都来源于leetcode
如有侵权请联系我删掉
今天的每日一题是:1704. 判断字符串的两半是否相似
给你一个偶数长度的字符串 s 。将其拆分成长度相同的两半,前一半为 a ,后一半为 b 。
两个字符串相似的前提是它们都含有相同数目的元音 (‘a’,‘e’,‘i’,‘o’,‘u’,‘A’,‘E’,‘I’,‘O’,‘U’)。注意,s 可能同时含有大写和小写字母。
如果 a 和 b 相似,返回 true ;否则,返回 false 。
示例 1:
输入:s = “book”
输出:true 解释:a = “bo” 且 b = “ok” 。a 中有 1 个元音,b 也有 1个元音。所以,a 和 b 相似。示例 2:
输入:s = “textbook”
输出:false
解释:a = “text” 且 b = “book” 。a 中有 1个元音,b 中有 2 个元音。因此,a 和 b 不相似。 注意,元音 o 在 b 中出现两次,记为 2 个。
简单模拟题:首先判断这个字符串的长度是否为偶数,如果是奇数的话就达不成上述条件。在偶数的情况下,使用一个指针指向s.size()/2的位置分开俩个字符串(可以另外用俩个字符串存贮,但是这里节省空间就没用),然后判断就行了。
class Solution {
public:
bool halvesAreAlike(string s) {
int n=s.size();
if(n%2!=0)
return false;
// string s1=s.substr(0,n/2);
// string s2=s.substr(n/2);
string yuanYin ="aeiouAEIOU";
int ans=0;
// for(int i=0;i
// {
// if(yuanYin.find_first_of(s1[i]) != string::npos)
// ans++;
// }
// for(int i=0;i
// {
// if(yuanYin.find_first_of(s2[i]) != string::npos)
// ans--;
// }
for(int i=0;i<n/2;i++)
{
if(yuanYin.find(s[i]) != string::npos)
ans++;
}
for(int i=n/2;i<n;i++)
{
if(yuanYin.find(s[i]) != string::npos)
ans--;
}
return ans==0;
}
};
了解了string:npos是代表字符串的最大长度,可以在匹配字符串或者替换字符串之类的场景使用。
104. 二叉树的最大深度
给定一个二叉树,找出其最大深度。
二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
说明: 叶子节点是指没有子节点的节点。
示例:
给定二叉树 [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7 返回它的最大深度 3 。
DFS:每次向下遍历维护一个max_depth变量。
BFS:同理。。
DFS:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int max_depth=0;
int maxDepth(TreeNode* root) {
dfs(root,0);
return max_depth;
}
void dfs(TreeNode* node ,int depth)
{
if(!node)
return;
depth++;
max_depth=max(max_depth,depth);
dfs(node->left,depth);
dfs(node->right,depth);
}
};
BFS:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int maxDepth(TreeNode* root) {
int depth=0;
if(!root)
return depth;
queue<TreeNode*> qu;
qu.push(root);
while(!qu.empty())
{
int n=qu.size();
depth++;
for(int i=0;i<n;i++)
{
TreeNode* node=qu.front();
qu.pop();
if(node->left)
qu.push(node->left);
if(node->right)
qu.push(node->right);
}
}
return depth;
}
};
巩固了DFS和BFS
111. 二叉树的最小深度
给定一个二叉树,找出其最小深度。
最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
说明:叶子节点是指没有子节点的节点。
示例 2:
输入:root = [2,null,3,null,4,null,5,null,6]
输出:5
首先明确根节点不应该是最短的路径(除非其没有子节点)
BFS:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int minDepth(TreeNode *root) {
if (root == nullptr) {
return 0;
}
queue<TreeNode *> que;
int depth=0;
que.push(root);
while (!que.empty()) {
int n=que.size();
depth++;
for(int i=0;i<n;i++)
{
TreeNode *node = que.front();
que.pop();
if (node->left == nullptr && node->right == nullptr) {
return depth;
}
if (node->left != nullptr) {
que.push(node->left);
}
if (node->right != nullptr) {
que.push(node->right);
}
}
}
return 0;
}
};
DFS
class Solution {
public:
int getDepth(TreeNode* node) {
if (node == NULL)
return 0;
int leftDepth = getDepth(node->left); // 左
int rightDepth = getDepth(node->right); // 右
// 中
// 当⼀个左⼦树为空,右不为空,这时并不是最低点
if (node->left == NULL && node->right != NULL) {
return 1 + rightDepth;
}
// 当⼀个右⼦树为空,左不为空,这时并不是最低点
if (node->left != NULL && node->right == NULL) {
return 1 + leftDepth;
}
int result = 1 + min(leftDepth, rightDepth);
return result;
}
int minDepth(TreeNode* root) {
return getDepth(root);
}
};
巩固DFS和BFS