笔试题 day1

目录

快速io

统计2的个数

 两个数组的交集

点击消除


快速io

import java.util.*;
import java.io.*;

public class Main
{
    public static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    public static Read in = new Read();
    
    public static void main(String[] args) throws IOException
    {
        // 写代码

        out.close();
    }
}


class Read // 自定义快速读入
{
    StringTokenizer st = new StringTokenizer("");
    BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    String next() throws IOException 
    {
        while(!st.hasMoreTokens())
        {
            st = new StringTokenizer(bf.readLine());
        }
        return st.nextToken();
    }
    
    String nextLine() throws IOException 
    {
        return bf.readLine();
    }
    
    int nextInt() throws IOException 
    {
        return Integer.parseInt(next());
    }
    
    long nextLong() throws IOException 
    {
        return Long.parseLong(next());
    }
    
    double nextDouble() throws IOException 
    {
        return Double.parseDouble(next());
    }
}

统计2的个数

        枚举出其中的每一项, 然后判断每一项中有多少个2, 然后计数即可: 

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int a = in.nextInt();
            int b = in.nextInt();
            
            int count = 0;
            for(int i = a; i <= b; i++) {
                int num = i;
                while (num != 0) {
                    if (num % 10 == 2) {
                        count ++;
                    }
                    num /= 10;
                }
            }
            System.out.print(count);
        }
    }
}

 两个数组的交集

暴力循环: 

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param nums1 int整型ArrayList 
     * @param nums2 int整型ArrayList 
     * @return int整型ArrayList
     */
    public ArrayList<Integer> intersection (ArrayList<Integer> nums1, ArrayList<Integer> nums2) {
        // write code here
        ArrayList<Integer> list = new ArrayList<>();

        for(int i = 0; i < nums1.size(); i++) {
            int var1 = nums1.get(i);
            for(int j = 0;j < nums2.size(); j++) {
                if (var1 == nums2.get(j)  && !list.contains(var1)){
                    list.add(var1);
                    continue;
                }
            }
        }
        return list;
    }
}

 哈希

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param nums1 int整型ArrayList
     * @param nums2 int整型ArrayList
     * @return int整型ArrayList
     */
    public ArrayList<Integer> intersection (ArrayList<Integer> nums1,
                                            ArrayList<Integer> nums2) {
        // write code here
        Set<Integer> set = new HashSet<>();
        for (int i = 0; i < nums1.size() ; i++  ) {
            set.add(nums1.get(i));
        }

        ArrayList<Integer> list = new ArrayList<>();
        for(int i = 0; i < nums2.size(); i++) {
            int var = nums2.get(i);
            if (set.contains(var)) {
                list.add(var);
                set.remove(var);
            }
        }
        return list;
    }
}

点击消除

 使用栈: LinkedList构建栈

import java.util.*;
import java.lang.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            String input = in.nextLine();
            LinkedList<Character> list = new LinkedList<>();
            int len = input.length();
            StringBuilder ret = new StringBuilder();
            for (int i = 0; i < len; i++) {
                char c = input.charAt(i);
                if (list.size() == 0) {
                    list.addLast(c);
                } else if (list.size() != 0 && list.getLast() == c) {
                    list.removeLast();
                    continue;
                } else {
                    list.add(c);
                }
            }

            for (char x : list) {
                ret.append(x);
            }
            if (ret.length() == 0) {
                System.out.println(0);
            } else {
                System.out.println(ret.toString());
            }
        }
    }
}

直接使用StringBuilder构建栈

import java.util.*;
import java.lang.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            String x = in.nextLine();
            
            StringBuilder ret = new StringBuilder();

            for(int i = 0; i < x.length(); i++) {
                if (ret.length() == 0) {
                    ret.append(x.charAt(i));
                } else if (ret.length() != 0 && ret.charAt(ret.length() - 1) == x.charAt(i)) {
                    ret.deleteCharAt(ret.length() - 1);
                } else {
                    ret.append(x.charAt(i));
                }
            }
            System.out.println(ret.length() == 0 ? 0 : ret.toString());
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值