zoukankan      html  css  js  c++  java
  • java 实例

    题目要求如下:

    第一行包含一个整数N,初始的元素的数量为L;

    第二行包含N个空格分隔的整数表示L;

    第三行包含一个整型的Q(查询的数量);

    2Q随后的行表示查询,和每个查询超过两行。

    1<=N<=4000

    1<=Q<=4000

    每个元素都是一个32位整数

    输入:

    5
    12 0 1 78 12  //L
    2     //需要输入的个数
    Insert    
    5 23    //将23放到位置5
    Delete
    0        //删除0

    输出:

    0 1 78 12 23

    输出最后的结果。

    代码如下:

    import java.util.Scanner;
    import java.util.LinkedList;
    
    public class Solution {
        public static void main(String[] args) {
            /* Create and fill Linked List of Integers */
            Scanner scan = new Scanner(System.in);
            int N = scan.nextInt();
            LinkedList<Integer> list = new LinkedList<>();
            for (int i = 0; i < N; i++) {
                int value = scan.nextInt();
                list.add(value);
            }
            
            /* Perfrom queries on Linked List */
            int Q = scan.nextInt();
            for (int i = 0; i < Q; i++) {
                String action = scan.next();
                if (action.equals("Insert")) {
                    int index = scan.nextInt();
                    int value = scan.nextInt();
                    list.add(index, value);
                } else { // "Delete"
                    int index = scan.nextInt();
                    list.remove(index);
                }
            }
            scan.close();
            
            /* Print our updated Linked List */
            for (Integer num : list) {
                System.out.print(num + " ");
            }
        }
    }
        
  • 相关阅读:
    ms08-067
    siem主流厂商
    技术设计
    SOC
    通过 IDE 向 Storm 集群远程提交 topology
    Storm
    java线程中Exchanger使用
    android笔记
    学习笔记 Java类的封装、继承和多态 2014.7.10
    POJ 2533 Longest Ordered Subsequence DP
  • 原文地址:https://www.cnblogs.com/Angella/p/6556543.html
Copyright © 2011-2022 走看看