逆下标映射

构造队列

http://www.nowcoder.com/questionTerminal/657d09e2b3704574814089ba8566d98d

把 0~n-1 放到初始数组里面,按照题中队列操作输出后,逆下标映射即可:

import java.util.*;
public class Main {
    public static void main(String[] args) throws Exception{
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        for(int i = 0; i < T; ++i){
            int n = sc.nextInt();
            List<Integer> v = new ArrayList<>();
            for(int j = 0; j < n; ++j) v.add(j);
            List<Integer> r = new ArrayList<>(v);
            List<Integer> ans = g(v);
            for(int j = 0; j < n; ++j){
                r.set(ans.get(j), j + 1);    //逆下标映射
            }
            f(r);
        }
    }
    static void f(List<Integer> v){    //打印输出
        System.out.print(v.get(0));
        for(int i = 1; i < v.size(); ++i){
            System.out.print(" ");
            System.out.print(v.get(i));
        }
        System.out.println();
    }
    static List<Integer> g(List<Integer> v){    //题给的队列操作
        List<Integer> ans = new ArrayList<>();
        while(!v.isEmpty()){
            int x = v.get(0);
            v.remove(0);
            v.add(x);
            x = v.get(0);
            ans.add(x);
            v.remove(0);
        }
        return ans;
    }
}
全部评论

相关推荐

05-07 13:29
已编辑
门头沟学院 Java
北斗导航Compass低仿版:能不能先搞清楚优先级啊,怎么可能是项目问题,项目很重要吗?又没学历 又没实习大厂凭啥约面?那玩具项目 没应用在真实生产环境下的 就算做上天又有什么用?早点找个小公司实习 拿小公司实习去投大厂实习,这才是你现在该做的
投递美团等公司10个岗位 简历被挂麻了,求建议
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务