前言:Java做算法题的弊端,力扣只需要完成一个函数,其他oj平台要求使用Scanner类在控制台输入输出,其中pat最后一个测试用例是大量数据,因此java在pat中经常超时,这时候可以采用带缓存的reader和buffer而不是System.in/out。
这个工具类,复制即可直接食用,都是静态方法,通过FastIO.xx就可以使用
方法名保持和Scanner/System.out一致
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
import java.io.*; import java.util.StringTokenizer;
public class FastIO { public static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in),32768); public static StringTokenizer tokenizer = new StringTokenizer(""); public static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
public static double nextDouble() throws IOException{ return Double.parseDouble(next()); } public static float nextFloat() throws IOException{ return Float.parseFloat(next()); } public static int nextInt() throws IOException{ return Integer.parseInt(next()); } public static String next() throws IOException{ while (!tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } public static String nextLine() throws IOException{ return reader.readLine(); } public static void println(String str){ out.println(str); out.flush(); } public static void printf(String format, Object ... args){ out.printf(format, args); out.flush(); } }
|