目录
快速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());
}
}
}