Java常用算法刷题模板 参考1:https://www.jianshu.com/p/11158dbc7bde
参考2:https://www.acwing.com/blog/content/593/
输入输出 简单输入 1 2 3 4 5 6 import java.util.Scanner import java.io.BufferedInputStream;Scanner sc1 = new Scanner(System.in); Scanner sc2 = new Scanner(new BufferedInputStream(System.in));
测试:
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 public static void main (String[] args) { long t1, t2, t3; Scanner sc1 = new Scanner(System.in); Scanner sc2 = new Scanner(new BufferedInputStream(System.in)); t1 = System.nanoTime(); String next = sc1.next(); t2 = System.nanoTime(); t3 = t2 - t1; System.out.println("sc1:" + t3); System.out.println(next); t1 = System.nanoTime(); String next2 = sc2.next(); t2 = System.nanoTime(); t3 = t2 - t1; System.out.println("sc2:" + t3); System.out.println(next2); }
复杂输入
1 2 3 4 5 6 7 8 9 10 11 12 public static void main (String[] args) { Scanner sc = new Scanner(System.in); String str = sc.next(); String[] split = str.split("," ); int [] strInt = new int [split.length]; for (int i = 0 ; i < split.length; i++) { strInt[i] = Integer.parseInt(split[i]); } System.out.println(Arrays.toString(strInt)); }
文件输入 用于大数据的读入,不用手动输入那么麻烦。BufferedInputStream缓冲流来加速,文件输入流用绝对路径 ,避免不必要的麻烦,要不就把资源文件放在src目录下。
1 2 3 4 5 6 7 8 9 public static void main (String[] args) throws FileNotFoundException { Scanner sc = new Scanner(new BufferedInputStream(new FileInputStream("E:\\input.txt" ))); List<Integer> list = new ArrayList<>(); while (sc.hasNext()) { list.add(sc.nextInt()); } System.out.println(list); }
快速读入 一般我常用的数据输入方法有两种,Scanner和BufferedReader。BufferedReader可以读一行,速度比Scanner快,所以数据较多的时候使用。注意BufferedReader用完记得关。
import建议按需单独导入,最好不要import xxx.*
全部导入,单独导入能提高编译速度和避免命名冲突。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
应用示例见蓝桥杯十四第一题。
Scanner
1 2 3 4 5 6 7 8 9 10 11 12 import java.util.*;public class Main { public static void main (String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int [] nums = new int [n]; for (int i = 0 ; i < n; i++) nums[i] = scan.nextInt(); } }
BufferedReader
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 import java.util.*;import java.io.*;public class Main { public static void main (String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(reader.readLine()); int [] nums = new int [n]; String[] strs = reader.readLine().split(" " ); for (int i = 0 ; i < n; i++) nums[i] = Integer.parseInt(strs[i]); reader.close(); } }
数据类型 字符串
按照空格分割字符串
1 2 3 String[] s=str.split(" +" ); String[] s=str.split("\\s+" );
public int indexOf(String str)
:返回字符串中第一次出现str的位置;
public int indexOf(String str,int fromIndex)
:返回字符串从fromIndex开始第一次出现str的位置;
public String substring(int beginIndex)
:返回该字符串从beginIndex开始到结尾的子字符串;
public String substring(int beginIndex,int endIndex)
:返回该字符串从beginIndex开始到endsIndex结尾的子字符串;
public char[] toCharArray ()
:将此字符串转换为新的字符数组;
public String replace (CharSequence target, CharSequence replacement)
:将与target匹配的字符串使用replacement字符串替换;
String replaceAll(String regex, String replacement)
:使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。
1 2 3 String ss=str.replaceAll(" +" , "," ); String ss=str.replaceAll("\\s+" , "," );
大数 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 BigDecimal(double val) BigDecimal(int val) BigDecimal(long val) BigDecimal(String val) BigInteger(int val) BigInteger(String val) BigInteger valueOf (long val) BigInteger (BigDecimal) .max (BigInteger(BigDecimal) ) BigInteger (BigDecimal) .min (BigInteger(BigDecimal) ) BigInteger (BigDecimal) .add (BigInteger(BigDecimal) ) BigInteger (BigDecimal) .subtract (BigInteger(BigDecimal) ) BigInteger (BigDecimal) .multiply (BigInteger(BigDecimal) ) BigInteger (BigDecimal) .divide (BigInteger(BigDecimal) ) BigInteger (BigDecimal) .mod (BigInteger(BigDecimal) ) BigInteger (BigDecimal) .abs ()
Calendar类
public static final int SUNDAY = 1;
public static final int JANUARY = 0;
以上表示,一周的开始是周日,即为1,以此类推。
一年的开始是一月,即为0,所以对月的设置要 - 1,对月的读取要 + 1
get(int field);
// 获取给定字段的值,field即上面的字段数字
1 2 3 4 5 6 7 8 9 10 Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.YEAR, 2020 ); calendar.set(Calendar.MONTH, 0 ); calendar.set(Calendar.DAY_OF_MONTH, 21 ); System.out.print(calendar.get(Calendar.DAY_OF_WEEK)); 输出: 3
Calendar类与Date类的转换 Date类 1 2 3 4 5 6 7 8 9 10 11 12 13 Date date = new Date(); System.out.println("毫秒:" +date.getTime()); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss" ); String time = sdf.format(date); System.out.println("时间转字符串:" +time); String time02 = "2018-09-05" ; SimpleDateFormat sdf2 = new SimpleDateFormat ("yyyy-MM-dd" ); Date date2 = sdf2.parse(time02); System.out.println("字符串转时间格式:" +date2);
Calendar类 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Calendar cal = Calendar.getInstance(); System.out.println("年:" + cal.get(Calendar.YEAR)); System.out.println("月:" + (cal.get(Calendar.MONTH) + 1 )); System.out.println("日:" + cal.get(Calendar.DAY_OF_MONTH)); System.out.println("时:" + cal.get(Calendar.HOUR_OF_DAY)); System.out.println("分:" + cal.get(Calendar.MINUTE)); System.out.println("秒:" + cal.get(Calendar.SECOND)); Calendar cal02 = Calendar.getInstance(); cal02.set(2018 ,9 ,1 ,12 ,0 ,0 ); System.out.println(cal02.getTime());
Calendar转换为Date 1 2 3 4 5 Calendar cal = Calendar.getInstance(); Date date = cal.getTime(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd" ); String s = simpleDateFormat.format(date); System.out.println("时间为====" +s);
Date转换为Calendar 1 2 3 4 Date date2 = new Date(); Calendar cal2 = Calendar.getInstance(); cal2.setTime(date2); System.out.println(cal2.get(Calendar.YEAR) +"-" +(cal2.get(Calendar.MONTH)+1 )+"-" +cal2.get(Calendar.DATE));
例题:
从键盘输入一个日期,格式为yyyy-M-d
要求计算该日期与1949年10月1日距离多少天
例如:
用户输入了:1949-10-2 程序输出:1
用户输入了:1949-11-1 程序输出:31
1 2 3 4 5 6 7 8 9 10 11 12 public static void main (String[] args) { Calendar cal = Calendar.getInstance(); Scanner sc = new Scanner(new BufferedInputStream(System.in)); String s = sc.next(); String[] str = s.split("-" ); cal.set(Integer.parseInt(str[0 ]), Integer.parseInt(str[1 ]) - 1 , Integer.parseInt(str[2 ])); Calendar standard = Calendar.getInstance(); standard.set(1949 , 10 - 1 , 1 ); long t1 = cal.getTimeInMillis(); long t2 = standard.getTimeInMillis(); System.out.println((t1 - t2) / (1000 * 60 * 60 * 24 ) + 1 ); }
双端队列 常用定义:
Deque<Integer> q=new LinkedList<Integer>();
LinkedList底层是链表,ArrayDeque底层是数组实现
offerFirst / addFirst(Object e);
队首插入
offerLast / addLast(Object e);
队尾插入
pollFirst();
队首删除
pollLast();
队尾删除
peekFirst();
队首获取
peekLast();
队尾获取
进制转换 其他进制字符串->十进制整数
public static Integer valueOf(int i)
public static Integer valueOf(String s)
public static Integer valueOf(String s, int radix) - radix为s字符串符合的进制
1 2 3 4 5 6 7 8 9 10 11 12 13 14 System.out.println(Integer.valueOf(12 )); System.out.println(Integer.valueOf("12" )); System.out.println(Integer.valueOf("12" ,10 )); System.out.println(Integer.valueOf("1100" , 2 )); System.out.println(Integer.valueOf("1100" , 8 )); System.out.println(Integer.valueOf("1100" , 16 ));
同理,用 public static int parseInt(String s)或 public static int parseInt(String s, int radix)转换为int型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 System.out.println(Integer.parseInt("12" )); System.out.println(Integer.parseInt("12" , 10 )); System.out.println(Integer.parseInt("1100" , 2 )); System.out.println(Integer.parseInt("1100" , 8 )); System.out.println(Integer.parseInt("1100" , 16 )); System.out.println(Integer.parseInt("1100" , 32 ));
十进制整数->其他进制字符串
public static String toBinaryString(int i) - 转二进制
public static String toOctalString(int i) - 转八进制
public static String toHexString(int i) - 转十六进制
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 int n = 12 ;System.out.println(Integer.toString(n)); System.out.println(Integer.toString(n, 2 )); System.out.println(Integer.toString(n, 8 )); System.out.println(Integer.toString(n, 16 )); System.out.println(Integer.toString(n, 32 ));
自定义排序 1 2 3 4 5 6 7 8 public static void main (String[] args) { String[] arr = new String[] {"3" , "22" , "1111" }; Arrays.sort(arr, (s1, s2) -> { return s2.length() - s1.length(); }); System.out.println(Arrays.toString(arr)); }
数组求和
其他常用模板 请参考:https://www.acwing.com/blog/content/593/
常用刷题API 参考1:https://www.cnblogs.com/chzhyang/p/13494554.html
参考2:https://blog.csdn.net/yubo_830/article/details/109112967
一个模板 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 41 42 43 44 45 46 47 48 49 50 51 import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.util.Arrays;class Main { static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static PrintWriter pw = new PrintWriter(System.out); static int N = 10010 ; static int n; public static void main (String[] args) throws IOException { n = Integer.parseInt(br.readLine()); for (int i = 0 ; i < n; i++) { String s[] = br.readLine().split(" " ); int x1 = Integer.parseInt(s[0 ]); int y1 = Integer.parseInt(s[1 ]); } pw.print(res); pw.flush(); pw.close(); br.close(); } class Node { int l, r; int cnt, len; public Node (int l, int r) { this .l = l; this .r = r; } } class Seg implements Comparable <Seg > { int x, y1, y2, k; public Seg (int x, int y1, int y2, int k) { this .x = x; this .y1 = y1; this .y2 = y2; this .k = k; } @Override public int compareTo (Seg seg) { return this .x - seg.x; } }