Sort by

recency

|

485 Discussions

|

  • + 0 comments

    Use stream

    import java.util.Arrays;
    import java.util.List;
    import java.util.Scanner;
    
    import static java.util.stream.Collectors.toList;
    
    public class Solution {
    
        private static final String STRING_INSERT = "Insert";
        private static final String STRING_DELETE = "Delete";
    
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
    
            int n = Integer.parseInt(scanner.nextLine()); // n is the number initial of elements in the list
    
            List<Integer> list = getIntegerList(scanner.nextLine());
    
            int numberOfQuerys = Integer.parseInt(scanner.nextLine());
    
            while (numberOfQuerys-- > 0) {
                String query = scanner.nextLine();
                List<Integer> queryList = getIntegerList(scanner.nextLine());
    
                if (query.equals(STRING_INSERT)) {
                    list.add(queryList.get(0), queryList.get(1));
                } else if (query.equals(STRING_DELETE)) {
                    list.remove((int) queryList.get(0)); // int -> index to delete, Integer -> object to delete
                }
            }
            scanner.close();
            list.forEach(number-> System.out.print(number + " "));
        }
    
        private static List<Integer> getIntegerList(String scanner) {
            return Arrays.stream(scanner.split(" ")).map(Integer::parseInt).collect(toList());
        }
    }
    
  • + 0 comments

    import java.io.; import java.util.; import java.text.; import java.math.; import java.util.regex.*; import java.util.ArrayList; import java.util.Arrays;

    public class Solution {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n =sc.nextInt();
        ArrayList<Integer> list = new ArrayList<>();
        for(int i=0;i<n;i++){
            list.add(sc.nextInt());
        }
        n=sc.nextInt();
        for(int i=0;i<n;i++){
            String str=sc.next();
            if(str.equals("Insert")){
                int a=sc.nextInt();
                int b=sc.nextInt();
                list.add(a, b);
            }
            if(str.equals("Delete")){
                int a = sc.nextInt();
                list.remove(a);
            }
        }for (int j : list) { System.out.print(j + " "); }
        sc.close();
    }
    

    }

  • + 0 comments

    my code: import java.io.; import java.util.;

    public class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
    
        int n = scan.nextInt();
        List<Integer> l = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            int x = scan.nextInt();
            l.add(x); 
        }
    
        int m = scan.nextInt();
        scan.nextLine(); 
    
        for (int i = 0; i < m; i++) {
            String s = scan.nextLine();
            if (s.equals("Insert")) {
                int a = scan.nextInt();
                int b = scan.nextInt();
                l.add(a, b);
                if (scan.hasNextLine()) {
                    scan.nextLine(); 
                }
            } else if (s.equals("Delete")) {
                int x = scan.nextInt();
                l.remove(x);
                if (scan.hasNextLine()) {
                    scan.nextLine(); 
                }
            }
        }
    
        for (Integer element : l) {
            System.out.print(element + " ");
        }
    }
    

    }

  • + 0 comments
    public class Solution {
    
        public static void main(String[] args) {
            Scanner in=new Scanner(System.in);
            int n=in.nextInt();
            List<Integer> list=new ArrayList<>(n); 
            for(int i=0;i<n;i++){
                list.add(in.nextInt());
            }  
            int q=in.nextInt();
            for(int j=0;j<q;j++){
                String str=in.next();
                if(str.equalsIgnoreCase("Insert")){
                    int x=in.nextInt();
                    int y=in.nextInt();
                    list.add(x,y);
                }
                if(str.equalsIgnoreCase("delete")){
                    int z=in.nextInt();
                    list.remove(z);
                }
            }
            for(int num:list){
                System.out.print(num+" ");
            }
        }
    }
    
  • + 0 comments

    the sample test case is passed but all other test cases got failed

    Scanner sc = new Scanner(System.in); int n = sc.nextInt();

        List<Integer> l = new ArrayList<>(n);
        for(int i =0; i<n; i++){
            l.add(sc.nextInt());
        }
        int q = sc.nextInt();
        sc.nextLine();
        for(int i =0; i<q; i++ ){
            String str = sc.nextLine().trim();
             if(str.equalsIgnoreCase("Insert")){
                int index = sc.nextInt();
                int value = sc.nextInt();
                sc.nextLine();
            l.add(index,value);
        } 
        if(str.equalsIgnoreCase("Delete")){
                int removeindex = sc.nextInt(); 
                 l.remove(removeindex);
           }
        }
           l.forEach(l1 -> System.out.print(l1 + " "));