牛客编程巅峰赛钻石王者组第五场
牛客编程巅峰赛钻石王者组第五场
链接说明
我好菜啊,第三题完全没思路。
1. 滑动窗口双指针
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
* 返回符合题意的最长的子串长度
* @param x string字符串
* @return int整型
*/
public int Maximumlength (String x) {
// write code here
int n = 0;
int p = 0;
int y = 0;
int l = 0;
int r = 0;
char[] chars = x.toCharArray();
int len = chars.length;
int maxLen = 0;
while(r < len){
char chr = chars[r];
if(chr == 'n'){++n;}
if(chr == 'p'){++p;}
if(chr == 'y'){++y;}
while(n * p * y != 0){
if(chars[l] == 'n'){--n;}
if(chars[l] == 'p'){--p;}
if(chars[l] == 'y'){--y;}
++l;
}
maxLen = Math.max(maxLen, r - l + 1);
++r;
}
return maxLen;
}
}2. 后缀表达式求值
使用栈模拟:
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
* 给定一个后缀表达式,返回它的结果
* @param str string字符串
* @return long长整型
*/
public long solve (String str) {
// write code here
long res = 0;
char[] chars = str.toCharArray();
int len = chars.length;
Deque<Long> stack = new ArrayDeque<>();
long num = 0;
for(int i = 0; i < len; ++i){
char ch = chars[i];
if(ch >= '0' && ch <= '9'){
num = num * 10 + ch - '0';
}else if(ch == '#'){
stack.push(num);
num = 0;
}else{
long a = stack.pop();
long b = stack.pop();
if(ch == '+'){
stack.push(a + b);
}else if(ch == '-'){
stack.push(b - a);
}else if(ch == '*'){
stack.push(a * b);
}
}
}
return stack.pop();
}
}#笔试题目#
查看10道真题和解析
360集团公司氛围 376人发布