携程后台笔试,求大佬指教指教

携程后台笔试,前两道死活都是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:"";
    }



#携程##笔试题目#
全部评论
你这第一题代码 思路惊奇 看不下去了~
点赞 回复 分享
发布于 2019-09-04 21:25
1. 第一题你可以先找到第一个小于等于m的节点记为start 2. 如果这个节点不是头节点,那么把这个节点拿出来,放到头节点的位置 3. 从start节点开始,从左向右找第一个小于等于m的节点 4. 把这个节点放到start的后面 5. 重复3 4结束
点赞 回复 分享
发布于 2019-09-04 21:31
友塔游戏
校招火热招聘中
官网直投
我跟你一样,两题都是71
点赞 回复 分享
发布于 2019-09-04 21:35
这是我第一题A了的代码 static ListNode partition(ListNode head, int m) { if (head == null || head.next == null) { return head; } ListNode head1 = new ListNode(m); ListNode head2 = new ListNode(m); ListNode pre = head1; ListNode after = head2; for (ListNode cur = head; cur != null; cur = cur.next) { if (cur.val > m) { after.next = cur; after = after.next; } else { pre.next = cur; pre = pre.next; } } if (head1.next == null) { return head; } else { pre.next = head2.next; after.next = null; return head1.next; } }
点赞 回复 分享
发布于 2019-09-06 20:24

相关推荐

zxxxxxr:不是挂了,是美团招聘特有机制,三天第一志愿没处理会自动结束跳转到第二志愿,以此类推,只要有人后面捞也会回到第一志愿的
投递美团等公司10个岗位
点赞 评论 收藏
分享
八极星:我看成了化身一团黑子哈哈哈😂
点赞 评论 收藏
分享
点赞 3 评论
分享
牛客网
牛客企业服务