一、每日一题
今天是个简单题,题意很明确,我们直接按照空格分成每一个子串,然后判断是不是符合标准就行了,if,else写了一堆。
class Solution {
public:
bool isvalid(string temp,int m)
{
int c1=0,c2=0;
for(int k=0;k<m;k++)
{
if(temp[k]=='!'||temp[k]=='.'||temp[k]==',')
{
if(k!=m-1) return false;
c1++;
}
if(temp[k]>='0'&&temp[k]<='9') return false;
if(temp[k]=='-')
{
c2++;
if(k==0||k==m-1) return false;
else if(!islower(temp[k - 1]) || !islower(temp[k + 1])) return false;
}
if(c1>1||c2>1) return false;
}
return true;
}
int countValidWords(string sentence) {
string temp;
int ans=0,l=0,r=0;
int n=sentence.size();
while (true) {
while (l < n && sentence[l] == ' ') {
l++;
}
if (l >= n) {
break;
}
r = l + 1;
while (r < n && sentence[r] != ' ') {
r++;
}
if (isvalid(sentence.substr(l, r - l),r-l)) {
ans++;
}
l = r + 1;
}
return ans;
}
};
二、永无止境DFS复习
很苦恼,昨天到今天一直在写DFS的题目,现在既不能说找到感觉了,也不能说完全没找到感觉,有些题能写出来,有些题即使知道用DFS也没思路。明天再杠他一天。
主要是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 minDepth(TreeNode* root) {
if(!root) return 0;
else if(!root->left&&!root->right) return 1;
int mindep=INT_MAX;
if(root->left)
{
mindep=min(minDepth(root->left),mindep);
}
if(root->right)
{
mindep=min(minDepth(root->right),mindep);
}
return mindep+1;
}
};
/**
* 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 cnt=0;
bool hasPathSum(TreeNode* root, int targetSum) {
if(!root) return false;
else if(!root->left&&!root->right)
{
return targetSum==root->val;
}
return hasPathSum(root->left, targetSum - root->val) ||
hasPathSum(root->right, targetSum - root->val);
}
};
/**
* 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:
vector<int> path;
vector<vector<int>> ret;
void dfs(TreeNode* root,int targetSum)
{
if (root == nullptr) {
return;
}
path.emplace_back(root->val);
targetSum -= root->val;
if (root->left == nullptr && root->right == nullptr && targetSum == 0) {
ret.emplace_back(path);
}
dfs(root->left, targetSum);
dfs(root->right, targetSum);
path.pop_back();
}
vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
dfs(root,targetSum);
return ret;
}
};
这个路径总和2的递归写法跟下面这个题一样。
/**
* 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:
vector<vector<int>> ret;
vector<int> path;
void add(TreeNode* root)
{
if(!root) return;
path.emplace_back(root->val);
if(!root->left&&!root->right) ret.emplace_back(path);
add(root->left);
add(root->right);
path.pop_back();
}
int sumNumbers(TreeNode* root) {
add(root);
int temp=0,ans=0;
for(int i=0;i<ret.size();i++)
{
for(int j=0;j<ret[i].size();j++)
{
temp = temp*10 + ret[i][j];
}
ans+=temp;
temp=0;
}
return ans;
}
};