목록Algorithm (127)
main
* Stack 사용 import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { Scanner scan = new Scanner(System.in); Stack stack = new Stack(); int N = scan.nextInt(); for (int i = 0; i < N; i++) { int num = scan.nextInt(); if (num != 0) { stack.push(num); } else { stack.pop(); } } long sum = 0; for (Integer i : stack) { sum += i; } System.out.p..
* 공차가 등차수열 import java.util.*; import java.io.*; public class Main { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); int num = sc.nextInt(); int d = 1; int sum = 0; int remain = 0; while (true) { if (sum + d == num) { remain = 0; break; } else if (sum + d > num) { remain = num - sum; break; } sum += d; d++; } if (remain != 0) { if (d % 2 == 1) { S..
* StringBuffer.reverse() 사용 import java.io.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str = br.readLine(); StringBuffer sb = new StringBuffer(str); String reversed_str = sb.reverse().toString(); if (str.equals(reversed_str)) { System.out.println(1); } else { System.out.println..
* 재귀 함수를 사용하여 풀었다. import java.util.*; import java.io.*; public class Main { static int[][] map; static int w; static int h; private static int landChecker(int i, int j) {// i - w , j - h int count = 0; try { if (i < w && j < h && map[i][j] == 1) { count = 1; map[i][j] = 0; landChecker(i - 1, j - 1); landChecker(i , j - 1); landChecker(i + 1, j - 1); landChecker(i - 1, j); landChecker(i + 1, j);..
import java.util.*; import java.io.*; public class Main { private static int groupWordChecker(String str) { char memory = '0'; int[] alphabets = new int['z' - 'a' + 1]; for (int j = 0; j < alphabets.length; j++) { alphabets[j] = 0; } for (char c : str.toCharArray()) { if (memory == c) { continue; } else if (alphabets[c - 'a'] == 0) { alphabets[c - 'a'] = 1; memory = c; continue; } else if (alpha..