/Algorithm_Study

๐Ÿ“š2020.01~ BOJ ๋ฌธ์ œํ’€์ด , ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ฐœ๋… ์ •๋ฆฌ

Primary LanguageJava

Algorithm_Study

  • BOJ ๋ฌธ์ œํ’€์ด 2020.01~
  • ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ฐœ๋… ์ •๋ฆฌ

Contents

์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ชฉ์ฐจ
Array
BackTracking
Data Structure
Divide And Conquer
Dynamic Programming
Graph
Greedy

Use

References

  • BOJ
  • SW Expert Academy

์ž…์ถœ๋ ฅ

โœ”๏ธ ์ž…๋ ฅ_BufferedReader

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));//์„ ์–ธ

String string = bufferedReader.readLine();      // ํ•œ์ค„์‹ ๋ฐ›์•„์˜ด
StringTokenizer stringTokenizer = new StringTokenizer(string);   //ํ† ํฌ๋‚˜์ด์ ธ๋ฅผ ํ†ตํ•ด ํŒŒ์‹ฑ, ๋„์–ด์“ฐ๊ธฐ ๋‹จ์œ„
int a = Integer.parseInt(stringTokenizer.nextToken()); 

์—ฌ๋Ÿฌ ์ค„ ์ž…๋ ฅ ๋ฐœ์„๋•Œ

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));

String string;
while((string=bufferedReader.readLine())!=null){
           // string ํ•œ์ค„
}
  • Buffered Stream : ๊ธฐ๋ณธ ์ž…์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ์— ๋ฒ„ํผ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•˜๋Š” ์ŠคํŠธ๋ฆผ
    • ์ž…๋ ฅ๋œ ๋ฐ์ดํ„ฐ๊ฐ€ ์ค‘๊ฐ„์— ๋ฒ„ํผ๋ง์ด ๋œ ํ›„์— ์ „๋‹ฌ๋˜๋ฉฐ, ์ถœ๋ ฅ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋ฒ„ํผ๋ฅผ ๊ฑฐ์ณ์„œ ์ถœ๋ ฅ๋Œ
    • ์ค‘๊ฐ„๋ฒ„ํผ๋ฅผ ์‚ฌ์šฉํ•จ์œผ๋กœ์จ ์‹œ์Šคํ…œ์˜ ๋ฐ์ดํ„ฐ์ฒ˜๋ฆฌ ํšจ์œจ์„ฑ์ด ๋†’์•„์ง!

โœ”๏ธ ์ž…๋ ฅ_Scanner

โœ”๏ธ ์ถœ๋ ฅ_BufferedWriter

BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(System.out)); //์„ ์–ธ

bufferedWriter.write("Hello World!");๋Œ

bufferedWriter.flush(); 
bufferedWriter.newLine();    // ์ค„๋ฐ”๊ฟˆ์ด ํ•„์š”ํ•  ๊ฒฝ์šฐ ์‚ฌ์šฉ
bufferedWriter.close();      // ๋ฒ„ํผ ๋‹ซ๊ธฐ
  • write() : ์ง์ ‘ ์ถœ๋ ฅ stream์— ๋ฐ˜์˜๋˜๋Š” ๊ฒƒ์ด X
  • flush(): ์„ฑ๋Šฅ์„ ์œ„ํ•ด buffer์— ์ €์žฅํ•ด ๋‘์—ˆ๋‹ค๊ฐ€ BufferedWriter๊ฐ€ flush๋˜๊ฑฐ๋‚˜ close๋˜์—ˆ์„ ๋•Œ ํ•œ๋ฒˆ์— ์ถœ๋ ฅ stream์— ๋ฐ˜์˜!
  • close() : ์•„์˜ˆ stream์„ ๋‹ซ์•„๋ฒ„๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์—, ๊ณ„์† ์ถœ๋ ฅ์„ ์›ํ•˜๋ฉด flush() ์‚ฌ์šฉ

โœ”๏ธ ์ถœ๋ ฅ_System.out.println()

  • ์ฝ˜์†” ์ถœ๋ ฅํ• ๋•Œ ์‚ฌ์šฉ, ๊ธฐ๋ณธ์ ์œผ๋กœ ๋””๋ฒ„๊ทธ๋ฅผ ์œ„ํ•œ ์ถœ๋ ฅ => Logger ์‚ฌ์šฉ ํ•„์š”
  • But ์‹œ์Šคํ…œ ๋ฆฌ์†Œ์Šค๋ฅผ ํ•„์š” ์ด์ƒ์œผ๋กœ ์žก์•„๋จน๋Š” ํ•œ๊ณ„๊ฐ€ ์กด์žฌ

JAVA DOC ์ธ์šฉ

System์€ Object ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์€ final ํด๋ž˜์Šค์ด๋‹ค. ์ธ์Šคํ„ด์Šคํ™” ํ• ์ˆ˜์—†๋‹ค. 
out์€ PrintStream ์˜ ์ธ์Šคํ„ด์Šค์ด๋ฉฐ, println์€ PrintStream์˜ ๋ฉ”์†Œ๋“œ์ด๋‹ค.
  • println์€ println -> print -> write() + newLine() ์ˆœ์„œ๋กœ ์ฒ˜๋ฆฌ๋Œ

๋ฌธ์ž์—ด ํฌ๋ฉงํŒ…

โœ”๏ธ String

String c = a + b;
  • + ์—ฐ์‚ฐ์ž๋ฅผ ํ†ตํ•ด ๋ฌธ์ž์—ด ์—ฐ๊ฒฐ ๊ฐ€๋Šฅ
  • ๐Ÿคจ ๋‚ด๋ถ€์ ์œผ๋กœ Autoboxing, Unboxing ๊ณผ์ •์„ ํ†ตํ•˜์—ฌ concat ๋ฉ”์†Œ๋“œ๋ฅผ ์ฐธ์กฐํ•ด ์‚ฌ์šฉํ•˜์—ฌ ๋Š๋ฆผ

_String์€ ๋ถˆ๋ณ€(immutable)ํ•œ ๊ฐ์ฒด์ด๊ธฐ ๋•Œ๋ฌธ์—, ๋‚ด๋ถ€์ ์œผ๋กœ ๊ณ„์†ํ•ด์„œ ์ƒˆ๋กœ์šด String ๊ฐ์ฒด๋“ค์ด ์ƒ์„ฑ๋˜์–ด ๋ฉ”๋ชจ๋ฆฌ ๋‚ญ๋น„๊ฐ€ ์‹ฌํ•จ

โœ”๏ธ StringBuilder

BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(System.out));

StringBuilder stringBuilder = new StringBuilder();

stringBuilder.append("์ฒซ๋ฒˆ์งธ ๋ฌธ์žฅ\n"); 
stringBuilder.append("๋‘๋ฒˆ ์žฌ ๋ฌธ์žฅ\n");
bufferedWriter.write(stringBuilder.toString());   // ์ถœ๋ ฅ ๊ฐ€๋Šฅํ•œ String ํ˜•ํƒœ๋กœ ๋ณ€ํ™˜

bufferedWriter.flush();
bufferedWriter.close();
  • StringBuilder : mutable ํ•œ ๊ฐ์ฒด์ด๋ฏ€๋กœ, append()๋ฅผ ํ†ตํ•ด ๋ถ™์ผ ์ˆ˜ ์žˆ์Œ
  • ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ณผ์ •์ด ์•„๋‹ˆ๋ฏ€๋กœ, ๋‚ด๋ถ€์ ์œผ๋กœ boxing ๊ณผ์ •์„ ๊ฑฐ์น˜์ง€ ์•Š์•„ ์†๋„๊ฐ€ ๋น ๋ฆ„

โœ”๏ธ StringBuffer

  • StringBuffer : StringBuilder์™€ ๋น„์Šทํ•˜์ง€๋งŒ, thread-safe ํ•œ ํŠน์ง•์ด ์žˆ์Œ
    • ์„œ๋ฒ„๋ฅผ ๊ตฌ์„ฑํ•˜๊ณ , ๋‹ค์ค‘์˜ ์‚ฌ์šฉ์ž๊ฐ€ ์ ‘๊ทผ ๊ฐ€๋Šฅํ•ด์•ผํ•  ๊ฒฝ์šฐ ์‚ฌ์šฉ
    • ๋‚ด๋ถ€์ ์œผ๋กœ Synchronization ์ ์šฉํ•˜๋Š” ๋กœ์ง์ด ์กด์žฌํ•ด ๋‹ค์†Œ ๋Š๋ฆผ