怕npy的牛牛
class Solution {
public:
/**
* 返回符合题意的最长的子串长度
* @param x string字符串
* @return int整型
*/
int Maximumlength(string x) {
// write code here
int l=-1,r=-1;
int maxn=0,flag1=0,flag2=0,flag3=0;
int n=x.size();
while(1)
{
while(r<n)
{
r++;
if(x[r]=='n')flag1++;
if(x[r]=='p')flag2++;
if(x[r]=='y')flag3++;
if(flag1&&flag2&&flag3)break;
}
maxn=max(maxn,r-l-1);
if(r==n)break;
while(l<n)
{
l++;
if(x[l]=='n')flag1--;
if(x[l]=='p')flag2--;
if(x[l]=='y')flag3--;
if(flag1==0||flag2==0||flag3==0)break;
}
}
return maxn;
}
};
牛牛与后缀表达式
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
* 给定一个后缀表达式,返回它的结果
* @param str string字符串
* @return long长整型
*/
long long solve(string str) {
// write code here
stack<long long> n;
long long s = 0, x = 0, y = 0;
for (int i = 0; i < str.size(); i++)
{
switch (str[i])
{
case '+':
x = n.top();
n.pop();
y = n.top();
n.pop();
n.push(x + y);
break;
case '-':
x = n.top();
n.pop();
y = n.top();
n.pop();
n.push(y - x);
break;
case '*':
x = n.top();
n.pop();
y = n.top();
n.pop();
n.push(x * y);
break;
case '/':
x = n.top();
n.pop();
y = n.top();
n.pop();
n.push(y / x);
break;
case '#':
n.push(s);
s = 0;
break;
default:
s = s * 10 + str[i] - '0';
break;
}
}
return n.top();
}
};
Tree III
class Solution
{
public:
int n;
int d[100010];
vector<int> v[100010];
inline void dfs(int x, int fa)
{
int sz = v[x].size();
for (int i = 0; i < sz; ++i)
{
if (v[x][i] == fa)
continue;
d[v[x][i]] = d[x] + 1;
dfs(v[x][i], x);
}
}
int tree3(vector<int> &E)
{
n = E.size();
for (int i = 0, x; i < n; ++i)
{
v[E[i]].push_back(i + 2), v[i + 2].push_back(E[i]);
}
++n;
int c = 0, e = 0, f = 0;
memset(d, 0, sizeof(d));
dfs(1, 1);
for (int i = 1; i <= n; ++i)
{
if (d[c] < d[i])
c = i;
}
memset(d, 0, sizeof(d));
dfs(c, c);
for (int i = 1; i <= n; ++i)
{
if (d[e] <= d[i])
f = e, e = i;
else if (d[f] <= d[i])
f = i;
}
int ans = d[f];
memset(d, 0, sizeof(d));
dfs(e, e);
c = f = 0;
for (int i = 1; i <= n; ++i)
{
if (d[c] <= d[i])
f = c, c = i;
else if (d[f] <= d[i])
f = i;
}
return max(ans, d[f]);
}
};