句子中的有效单词数
class Solution {
private:
unordered_set<char> symbols={'!','.',','};
public:
bool isLowChar(char ele){
if(ele <= 'z' && ele >= 'a')
return true;
return false;
}
bool count(string& s){
int countSymbol1=0;//'-'
int countSymbol2=0;//'!'、'.' 和 ','
int index1=0,index2=0;
int n=s.size();
for(int i=0;i<s.size();i++){
if(isdigit(s[i]) ){
cout<<"failed isdigit : "<<s[i]<<endl;
return false;
}
if(isLowChar(s[i])==false && s[i]!='-' && !symbols.count(s[i])){
cout<<"failed isLowChar : "<<s[i]<<endl;
bool cond1=(isLowChar(s[i])==false);
bool cond2=(s[i]!='-');
bool cond3=(symbols.count(s[i]));
bool wholecond=isLowChar(s[i])==false || s[i]!='-' || symbols.count(s[i]);
cout<<"cond1 : "<<cond1<<endl;
cout<<"cond2 : "<<cond2<<endl;
cout<<"cond3 : "<<cond3<<endl;
cout<<"wholecond : "<<wholecond<<endl;
return false;
}
if(s[i]=='-'){
++countSymbol1;
index1=i;
}
if(symbols.count(s[i])){
++countSymbol2;
index2=i;
}
if( countSymbol1>1 || countSymbol2>1)
return false;
}
bool condition1= countSymbol1==1 && (index1>0 && index1<n-1) && isLowChar(s[index1-1]) && isLowChar(s[index1+1]);
bool condition2= countSymbol2==1 && index2==(n-1);
if(countSymbol2==0 && condition1)
return true;
if(countSymbol1==0 && condition2)
return true;
if(condition1 && condition2)
return true;
if(countSymbol1==0 &&countSymbol2==0)
return true;
return false;
}
vector<string> splitSentence(string sentence){
sentence.push_back(' ');
vector<string> res;
string tmp="";
for(auto ele : sentence){
if(ele != ' ')
tmp.push_back(ele);
else if(tmp.size()){
res.push_back(tmp);
tmp.clear();
}
}
cout<<"sentence size : "<<res.size()<<endl;
return res;
}
int countValidWords(string sentence) {
vector<string> ans=splitSentence(sentence);
int res=0;
for(auto ele:ans){
bool tmp=count(ele);
res+=tmp;
cout<<ele<<" "<<tmp<<endl;
}
cout<<endl;
cout<<" res : "<<res<<endl;
return res;
}
};
下一个更大的数值平衡数
class Solution
{
private:
vector<int> ans;
vector<int> base = {1, 22, 333, 4444, 55555, 666666, 7777777};
public:
void makeData(int input)
{
getInfo(input);
processPakg();
for (auto ele : s)
{
sort(ele.begin(), ele.end());
// cout<<"cur str : "<<ele<<endl;
string tmp = "";
do
{
tmp.clear();
for (auto elechar : ele)
{
tmp.push_back(elechar);
}
// cout<<"tmp : "<<tmp<<endl;
ans.push_back(atoi(tmp.c_str()));
// cout << " cur ans : " << ans.back() << endl;
} while (next_permutation(ele.begin(), ele.end()));
}
sort(ans.begin(), ans.end());
// cout << " ans size : " << ans.size() << endl;
}
int nextBeautifulNumber(int n)
{
makeData(n);
for (auto ele : ans)
{
if (ele > n)
return ele;
}
return -1;
}
void getInfo(int input){
string tmp = to_string(input);
int n = tmp.size();
if (input >= base[n - 1])
++n;
dfs(0,0,s,n);
// cout<<" pakg size : "<<pakg.size()<<endl;
}
void showStr(){
processPakg();
for (auto ele : s)
{
cout<<ele<<" ";
}
cout<<endl;
}
private:
vector<string> choose = {"1", "22", "333", "4444", "55555", "666666"};
vector<bool> memo={false,false,false,false,false,false};//表示未被选中
vector<string> s;
vector<vector<string>> pakg;
void processPakg(){
string tmp="";
for (auto ele : pakg)
{
for (auto substr : ele)
{
tmp+=substr;
}
s.push_back(tmp);
tmp.clear();
}
}
void dfs(int size,int index,vector<string> s,int target){
if(size==target){
pakg.push_back(s);
return;
}
for (int i = index; i < choose.size(); i++)
{
if (!memo[i])
{
memo[i]=true;
s.push_back(choose[i]);
dfs(size+i+1,i,s,target);
s.pop_back();
memo[i]=false;
}
}
}
};
统计最高分的节点数目
啊 直接摆烂
并行课程 III
class Solution
{
private:
vector<vector<int>> relations;
vector<int> time;
int n = 0;
int res = 0;
public:
vector<int> getFinalClass(vector<vector<int>> &relations)
{
unordered_set<int> outNodeSet;
unordered_set<int> inNodeSet;
vector<int> res;
for (auto ele : relations)
{
if (!outNodeSet.count(ele[1]))
outNodeSet.insert(ele[1]);
if (!inNodeSet.count(ele[0]))
inNodeSet.insert(ele[0]);
}
for (auto ele : outNodeSet)
if (!inNodeSet.count(ele))
res.push_back(ele);
cout << "final class size : " << res.size() << endl;
return res;
}
int minimumTime(int n, vector<vector<int>> &relations, vector<int> &time)
{
if(relations.empty())
return *max_element(time.begin(),time.end());
//思路:l1,l2,l3....节点可以到达n节点
//所以到达n节点的最短路径,等于到达l1,l2,l3节点的最长路径+节点n的时间
this->relations = relations;
this->time = time;
this->n = n;
vector<int> finalNode = getFinalClass(relations);
for (auto node : finalNode)
{
dfs(node, 0);
}
cout << " res : " << res << endl;
return res;
}
void dfs(int cur, int prevtime)
{
//首先找到到达cur
vector<int> fromNode;
for (auto ele : relations)
if (ele[1] == cur)
fromNode.push_back(ele[0]);
if (fromNode.empty())
{
//说明已经是最外层的点了
res = max(res, prevtime + time[cur - 1]);
cout << " 最外层点 : " << cur << " , res : " << res << endl;
}
else
{
cout << " show fromNode : ";
for (auto ele : fromNode)
cout << ele << " ";
cout << endl;
for (auto ele : fromNode)
{
dfs(ele, prevtime + time[cur - 1]);
}
}
}
};
总结
这周的周赛第一题直接把我做崩了,明明很简单,考虑不周到…
上周最后一题也是图…后面抽空刷一刷专题
心态炸裂
anyway
good luck.