携程后台3A代码



第一题:类似链表排序
    public static ListNode partition(ListNode head, int m) {
        ListNode sB = null;// 小于等于m的链表起点
        ListNode sE = null;//小于等于m的链表终点
        ListNode bB = null;
        ListNode bE = null;
        ListNode next = null;//保存下一个节点
        //进行划分
        while (head != null) {
            next = head.next;
            head.next = null;
            if (head.val <= m) {
                if (sB == null) {
                    sB = head;
                    sE = head;
                } else {
                    sE.next = head;
                    sE = sE.next;
                }
            } else {
                if (bB == null) {
                    bB = head;
                    bE = head;
                } else {
                    bE.next = head;
                    bE = bE.next;
                }
            }
            head = next;
        }
        //串联
        if (sB != null) {
            sE.next = bB;
        }
        return sB != null ? sB : bB;
    }
第二题:反转字符串
    static String resolve(String expr) {

        Stack<Character> stack = new Stack<>();
        int t = 0;// 统计'('的个数

        for(int i = 0; i < expr.length(); i++){
            if(expr.charAt(i) == '('){
                stack.push(expr.charAt(i));
                t++;
            }else if(expr.charAt(i) != ')'){
                stack.push(expr.charAt(i));
            }else{
                if(t < 1){
                    return "";
                }
                // 开始旋转
                StringBuilder builder = new StringBuilder();
                while(stack.peek() != '('){
                    builder.append(stack.peek());
                    stack.pop();
                }
                stack.pop();
                t--;
                // 放进去
                String s = builder.toString();
                for(int j = 0; j < s.length(); j++){
                    stack.push(s.charAt(j));
                }
            }
        }
        if(t != 0){
            return "";
        }
        char[] str = new char[stack.size()];
        int i = stack.size() - 1;
        while(!stack.isEmpty()){
            str[i--] = stack.peek();
            stack.pop();
        }
        return new String(str);
    }

第三题:任务调度,采用二分法
    static int schedule(int m,int[] array) {
        int n = array.length;
        int max = 0;
        int sum = 0;
        for(int i = 0; i < n; i++){
            max = Math.max(max, array[i]);
            sum += array[i];
        }
        if(m >= n)
            return max;

        int target = bSearch(max, sum, m, array);
        return target;
    }

    static int bSearch(int lower, int upper, int m, int[] array){
        int n = array.length;
        int mid = 0;

        while (lower <= upper){
            mid = (lower + upper) >> 1;
            int curSum = 0;
            int count = 0;
            boolean flag = true;

            for(int i = 0; i < n;){
                while (i < n && curSum + array[i] <= mid){
                    curSum += array[i++];
                }
                count++;
                if(count > m){
                    flag = false;
                    break;
                }
                curSum = 0;
            }
            // 是否可以继续缩小
            if(flag){
                upper = mid - 1;
            }else{
                lower = mid + 1;
            }
        }
        return mid;
    }



#携程##笔试题目#
全部评论
第三题思路是什么样的
点赞 回复 分享
发布于 2019-09-04 21:03
可以简单讲一下第二题思路吗?我只过了86
点赞 回复 分享
发布于 2019-09-04 21:04
我用的是两个栈,后面也加了对括号不匹配的判断,但就是死活就是57 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:16
点赞 回复 分享
发布于 2019-09-04 21:18

相关推荐

2024-12-29 11:08
湖南工业大学 Java
程序员牛肉:简历没什么大问题了。 而且不要再换项目了。三月份就开暑期实习了,现在都一月份了。实在来不及重新开一下项目了。把一个项目写完或许很快,但是把一个项目搞懂吃透并不简单。所以不要换项目了,把你简历上面的两个项目好好挖一挖吧。 具体 体现在:你能不能流利的说出你的项目的每一个功能点代码实现?你能不能说出在这块除了A技术之外,还有其他技术能够实现嘛?如果有其他技术能够实现,那你这块为什么选择了你当前用的这个技术?
投递牛客等公司
点赞 评论 收藏
分享
02-16 22:13
门头沟学院 Java
Yki_:女生学成这样挺不错了,现在停止网课,立刻all in八股,从最频繁的开始背,遇到不会的知识点直接问AI,项目也别手敲,直接看技术文档,背别人总结好的面试官可能问的问题的答案,遇到不会的再去代码里找具体实现就可以了,3月份开始边背边投实习约面
点赞 评论 收藏
分享
评论
4
26
分享

创作者周榜

更多
牛客网
牛客企业服务