题解 | #操作序列#

操作序列

http://www.nowcoder.com/practice/b53bda356a494154b6411d80380295f5

你们的方法都好高端,我只想到了用链表的方式来解决这个问题
意外的内存和速度都比较好

import java.util.*;
public class Main {
        public static void main(String[] args){
            Scanner sc=new Scanner(System.in);
            int n=sc.nextInt();
            ListNode head=new ListNode(sc.nextInt());
            ListNode foot=head;
            if(n%2==0){
            for(int i=1;i<n;i++){
                ListNode temp=new ListNode(sc.nextInt());
                if(i%2==0){
                    foot.next=temp;
                    foot=temp;
                }else{
                    temp.next=head;
                    head=temp;
                }
            }}else{
                for(int i=1;i<n;i++){
                    ListNode temp=new ListNode(sc.nextInt());
                    if(i%2==1){
                        foot.next=temp;
                        foot=temp;
                    }else{
                        temp.next=head;
                        head=temp;
                    }
                }
            }

            for(int i=0;i<n;i++){
                System.out.print(head.date);
                System.out.print(" ");
                head=head.next;
            }
        }
}
class ListNode{
    public int date;
    public ListNode next;
    ListNode(int da){
        this.date=da;
    }
}
全部评论

相关推荐

01-24 04:44
门头沟学院 Java
数学转码崽:项目感觉有点简单,再加上学历不是92的话,大厂实习很难过筛吧,即使给几个面试,感觉也通过不了,还是放低预期,先去中厂沉淀吧,暑期实习可以试着冲大厂,如果非大厂不去的话,不如去考研,双非学历真的硬伤
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务