apply


私信TA

用户名:uq_71543461426

访问量:1207

签 名:

等  级
排  名 3609
经  验 1809
参赛次数 0
文章发表 6
年  龄 0
在职情况 学生
学  校
专  业

  自我简介:

TA的其他文章

解题思路:

我们用两个list 记录一下左移的数 和 右移的数 但是 一个数可能重复移动多次 所以针对这种情况 开一个int数组记录一下最后一次这个数是左移还是右移 左移我们赋值为1 右移赋值为2 如果没移动 保持0即可

对于记录左移的数的list我们命名成l  对于l来说:

因为越往后左移的数越先输出 所以倒序遍历一下l,开一个boolean数组记录一下这个数有没有被输出,判断一下在int数组状态是否是1 并且没有被用过 就输出 然后把i标记成已使用

对于记录右移的数的list我们命名成r 对于r来说:

因为越往后右移的数靠后输出,所以正序遍历r,注意一下,一个数可能多次右移,所以首先开一个HashMap,判断一下在int数组状态是否为2 如果是2 hashMap里面存储 key是当前数 value 是下标,遍历完把hashMap排序一下,然后顺序输出即可。


注意事项: 主要问题是一个数可能被多次移动,所以需要判断这个数在左移列表里面和右移列表里面不同情况。

参考代码:

import java.util.*;
import java.io.*;
public class Main {
    public static void main(String[] args) throws Exception{
        BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
        String[] s = cin.readLine().split(" ");
        int n = Integer.parseInt(s[0]);
        int m = Integer.parseInt(s[1]);
        List<Integer> l = new ArrayList<>();
        List<Integer> r = new ArrayList<>();
        int[] status = new int[n + 1];
        boolean[] used = new boolean[n + 1];
        while(m -- > 0) {
            s = cin.readLine().split(" ");
            String s1 = s[0];
            int x = Integer.parseInt(s[1]);
            if(s1.equals("L")) {
                l.add(x);
                status[x] = 1;
            } else {
                r.add(x);
                status[x] = 2;
            }
             
        }
        for(int i = l.size() - 1; i >= 0; i--) {
            if(!used[l.get(i)] && status[l.get(i)] == 1) {
                System.out.print(l.get(i) + " ");
                used[l.get(i)] = true;
            }
        }
        for(int i = 1; i <= n; i++) {
            if(status[i] == 0) System.out.print(i + " ");
        }
        HashMap<Integer, Integer> hashMap = new HashMap<>();
         
        for(int i = 0; i < r.size(); i++) {
            if(!used[r.get(i)] && status[r.get(i)] == 2) {
                hashMap.put(r.get(i), i);
            }
        }
        List<Integer> sorted = new ArrayList<>(hashMap.values());
        Collections.sort(sorted);
        for(int key : sorted) {
            System.out.print(r.get(key) + " ");
        }
    }
}


 

0.0分

8 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区

请问一些,右移不能根据list的索引顺序输出吗
2024-04-06 20:07:58
  • «
  • 1
  • »