首页 > 360第二题,忘记双端队列,自己手写了一个
头像
常在面试的牛客人士
编辑于 2020-08-22 23:40
+ 关注

360第二题,忘记双端队列,自己手写了一个

package com.kuang.demo01;

import java.util.Scanner;

public class Main {

    static class ListK{
        int val;
        ListK pre;
        ListK next;
        public ListK(){
        }
        public ListK(int val){
            this.val = val;
        }
    }

    static ListK head = new ListK();
    static ListK nail = new ListK();

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while(in.hasNext()){
            int n = in.nextInt();
            int m = in.nextInt();
            ListK headp = head;
            for(int i = 1 ; i <= n; i++){
                ListK newNode = new ListK(i);
                headp.next = newNode;
                newNode.pre = headp;
                headp = newNode;
            }

            headp.next = nail;
            nail.pre = headp;
            int mcount = 0;
            for(int i = 0 ;i < m; i++){
                int op = in.nextInt();

                if(op == 1){
                    if(mcount != 0){
                        if(mcount % 2 == 0){

                        }else{
                            headp = head.next;
                            while(headp != nail){
                                int tempVal = headp.val;

                                headp.val = headp.next.val;
                                headp.next.val = tempVal;
                                headp = headp.next.next;
                            }
                        }
                        mcount = 0;

                    }else{

                    }
                    ListK moveNode = head.next;
                    head.next.next.pre = head;
                    head.next = head.next.next;

                    nail.pre.next = moveNode;
                    moveNode.pre = nail.pre;
                    moveNode.next = nail;
                    nail.pre = moveNode;

                }else{
                    mcount ++;
                }
            }
            if(mcount != 0){
                if(mcount % 2 == 0){

                }else {
                    headp = head.next;
                    while (headp != nail) {
                        int tempVal = headp.val;

                        headp.val = headp.next.val;
                        headp.next.val = tempVal;
                        headp = headp.next.next;
                    }
                }
            }
            head = head.next;
            while(head != nail){
                System.out.print(head.val+" ");
                head = head.next;
            }
            System.out.println();

        }
    }
}

全部评论

(1) 回帖
加载中...
话题 回帖

推荐话题

相关热帖

近期热帖

历年真题 真题热练榜 24小时
技术(软件)/信息技术类
查看全部

近期精华帖

热门推荐