16单选,10道多选【很杂,乱写】,4道专项c++/java/python,两道编程
第一题
变态跳台阶问题,然鹅不知道为啥一直0,搞了我50分钟
第二题
撤回和恢复
输入【空格或tab分隔】
hello undo redo world输出
hello world
思路:两个栈
通过80%,tab的问题,不知道为啥用正则表达式\\s+还是没包含tab
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String[] strs = sc.nextLine().split("\\s+"); System.out.println(Cal(strs)); } public static String Cal (String[] strs) { Stack<String> stack1 = new Stack<String> (); Stack<String> stack2 = new Stack<String> (); for (String str:strs){ if (str.equals("undo")) { if (stack2.size()>0) { stack1.push(stack2.pop()); } } else if (str.equals("redo")) { if (stack1.size()>0) { stack2.push(stack1.pop()); } } else { stack2.push(str); } } String[] s = new String[stack2.size()]; int i = stack2.size()-1; while (stack2.size()>0) { s[i] = stack2.pop(); --i; } StringBuilder sb = new StringBuilder(); for (int j = 0; j<s.length; ++j) { sb.append(s[j]); if (j < s.length-1) { sb.append(" "); } } return sb.toString(); } }
全部评论
(9) 回帖