携程后台笔试,求大佬指教指教
携程后台笔试,前两道死活都是57,有没有大佬帮忙康康指教指教,感激不尽
第一道:
// 给定一个单向链表和一个整数m,将链表中小于等于m的节点移到大于m的节点之前,要求两部分中的节点各自保持原有的先后顺序 // 样例输入 // 4 // 9 6 3 7 6 5 // 样例输出 // 3,9,6,7,6,5 static ListNode partition(ListNode head,int m) { if(head==null) return null; ListNode temp=new ListNode(m),newHead=null,p1=new ListNode(-1),p2=new ListNode(-1); p1.next=temp; p2=temp; while(head!=null){ ListNode next=head.next; if(head.val<=m){ p1.next=head; head.next=temp; p1=head; if(newHead==null) newHead=head; }else{ p2.next=head; p2=p2.next; } head=next; } p1.next=temp.next; return newHead; }第二道:
// 豚厂给自研的数据库设计了一套查询表达式,在这个表达式中括号表示将里面的字符串翻转。请你帮助实现这一逻辑。括号不匹配,输出空字符
// 样例输入
// ((ur)oi)
// 样例输出
// iour
static String resolve(String expr) {
if(expr==null)
return null;
Stack<Integer> stack1=new Stack<>();
Stack<Integer> stack2=new Stack<>();
for(int i=0;i<expr.length();i++){
char c=expr.charAt(i);
if(c!=')'){
stack1.add((int)c);
}else{
if(!stack1.isEmpty()){
while(stack1.peek()!=(int)'(')
stack2.add(stack1.pop());
stack1.pop();
while(!stack2.isEmpty()){
stack1.add(stack2.pop());
}
}
}
}
String rs="";
boolean match=true;
while(!stack1.isEmpty()){
if(stack1.peek()=='('||stack1.peek()==')'){
match=false;
break;
}
rs+=stack1.pop();
}
return match?rs:"";
} 
