본문 바로가기

728x90
반응형

알고리즘

(34)
[LeetCode/24/Java]Swap Nodes in Pairs [LeetCode/24/Java]Swap Nodes in Pairs 풀이 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */ class Solution { public ListNode swapPairs(ListNode head) { if((head == null) || (head.next == null)) return head; ListNode re..
[LeetCode/344/Java]Reverse String [LeetCode/344/Java]Reverse 풀이 class Solution { public void reverseString(char[] s) { reverseString(s, 0); } public void reverseString(char[] s, int idx) { if(idx < s.length / 2) { char temp1 = s[idx]; s[idx] = s[(s.length - 1) - idx]; s[(s.length - 1) - idx] = temp1; reverseString(s, idx + 1); } } } 후기 문자열을 뒤집는 처리입니다. 반복문을 사용하는 방법이 일반적이지만 재귀 호출을 연습할 겸 재귀호출을 이용한 방법을 사용해 보았습니다. time complexity O(N..
[백준/10870/Java]피보나치 수 [백준/10870/Java]피보나치 수 풀이 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int N = Integer.parseInt(br.readLine()); int result = 0; int prev = 1; for (int i = 0; i < N; i++) { result += prev; prev = re..
[백준/10872/Java]팩토리얼 [백준/10872/Java]팩토리얼 풀이 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int N = Integer.parseInt(br.readLine()); int result = 1; for (int i = 1; i
[백준/2581/Java]소수 [백준/2581/Java]소수 풀이 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int M = Integer.parseInt(br.readLine()); int N = Integer.parseInt(br.readLine()); boolean[] bool = new boolean[N + 1]; int sum = 0;..
[백준/1978/Java]소수찾기 [백준/1978/Java]소수찾기 풀이 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int N = Integer.parseInt(br.readLine()); String[] str = br.readLine().split(" "); int count = 0; for (int i = 0; i < N; i++) { in..
[백준/10250/Java]ACM호텔 [백준/10250/Java]ACM호텔 풀이 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int intr = Integer.parseInt(br.readLine()); for (int i = 0; i < intr; i++) { String[] arr = br.readLine().split(" "); int H = I..
[백준/1065/Java]한수 [백준/1065/Java]한수 풀이 import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; public class Main { public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(Syst..

728x90
반응형