본문 바로가기
알고리즘

자바 알고리즘에서 자주 쓰이는 코드

by Jason95 2021. 7. 26.

 

https://redbinalgorithm.tistory.com/579

https://real-012.tistory.com/152

 

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

public class Main {
    static FastIO io = new FastIO();

    public static void main(String[] args) throws IOException {
        try {
        	// 문제 풀이 코드
        }
        catch(Exception e) {
            e.printStackTrace();
        }
        finally {
            io.flush();
            io.close();
        }
    }
}

class FastIO {
    static BufferedReader br;
    static BufferedWriter bw;
    static StringTokenizer st;

    FastIO() {
        br = new BufferedReader(new InputStreamReader(System.in));
        bw = new BufferedWriter(new OutputStreamWriter(System.out));
    }

    String next() throws IOException {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }

    String nextString() throws IOException {
        return next();
    }

    int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }

    String nextLine() throws IOException {
        return br.readLine();
    }

    void write(double d) throws IOException {
        bw.write(String.valueOf(d));
    }

    void write(char c) throws IOException {
        bw.write(c);
    }

    void write(int i) throws IOException {
        bw.write(String.valueOf(i));
    }

    void write(long l) throws IOException {
        bw.write(String.valueOf(l));
    }

    void write(StringBuilder sb) throws IOException {
        bw.write(sb.toString().trim());
    }

    void write(String str) throws IOException {
        bw.write(str.trim());
    }

    void flush() throws IOException {
        bw.flush();
    }

    void newLine() throws IOException{
        bw.newLine();
    }

    void close() throws IOException{
        br.close();
        bw.close();
    }
}

Math.max(1,2)

 

 

<정렬>

List list = new ArrayList<>();
Collections.sort(list);

int[] arr = new int[10];
Arrays.sort(arr); // 오름차순

Integer[] arr = new Integer[10];
Arrays.sort(arr, Collections.reverseOrder()); // 내림차순
class Pair {
    char c;
    int i;
    public Pair(char c, int i) {
        this.c = c;
        this.i = i;
    }
}

class PairComparator implements Comparator<Pair> {
    @Override
    public int compare(Pair p1, Pair p2) {
        return p2.i - p1.i; // 내림차순
    }
}

public class Main {
    static List<Pair> list = new ArrayList<>();

    public static void main(String[] args) throws IOException {
    	Collections.sort(list, new PairComparator());
    }
}

5의 2승 = Math.pow(5, 2)

// 숫자가 작을수록 우선순위가 높음
PriorityQueue<Integer> pq = new PriorityQueue<>();

// 숫자가 클수록 우선순위가 높음
PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());

pq.add(1); // 삽입
pq.remove(); // 우선순위가 가장 높은 값 제거
pq.peek(); // 우선순위가 가장 높은 값 참조

<ArrayList -> Array 전환>

List<Integer> list = new ArrayList<Integer>();
int[] arr = list.stream().mapToInt(i -> i).toArray();
ArrayList<String> list = new ArrayList();
Object[] objects = list.toArray();
String[] arr = new String[list.size()];
for (int i = 0; i < objects.length; i++) {
	arr[i] = (String) objects[i];
}

<Wrapper 클래스의 비교>

Integer, Long 등의 Wrapper 클래스는

== 가 아닌 equals를 쓸 것

== : 주소 비교

equals : 값 비교

참고 : https://marobiana.tistory.com/130

참고2 : https://dololak.tistory.com/43