Algorithms
-
LeetCode - Same Tree [Java]Algorithms/- LeetCode 2022. 2. 8. 22:39
문제 링크 : Same Tree Same Tree - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 정답(Solution) /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, Tr..
-
LeetCode - Binary Tree Inorder Traversal [Java]Algorithms/- LeetCode 2022. 2. 7. 22:56
문제 링크 : Binary Tree Inorder Traversal Binary Tree Inorder Traversal - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 정답(Solution) Stack 풀이 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(in..
-
재귀 알고리즘 분석 [자바]Algorithms 2022. 2. 7. 22:10
재귀란? 자신을 정의할 때 자기 자신을 재참조하는 방법이다. 알고리즘을 작성할 때 재귀를 이용하여 복잡한 로직을 쉽게 구현할 수 있다. 이렇게 정의한 경우 재귀적(recursive)이라고 한다. 팩토리얼 재귀의 예로 가장 많이 소개되는 팩토리얼이다. public int factorial(int n){ if(n > 0){ return n * factorial(n-1); }else{ return 1; } } 위와 같이 factorial 함수를 정의할 때 자기 자신을 재참조하여 작성할 수 있다. 재귀 호출 위 factorial 함수에 3을 대입하여 예시 코드가 어떻게 호출되는지 확인해 보자. n = 3 ↓ factorial(3) 호출 ↓ if( 3 > 0 ) return 3 * factorial( 3 - 1 ..
-
프로그래머스 - 단체사진 찍기 [자바] (feat. += , + 차이)Algorithms/- 프로그래머스 2022. 2. 6. 22:18
문제 링크 : 단체사진 찍기 코딩테스트 연습 - 단체사진 찍기 단체사진 찍기 가을을 맞아 카카오프렌즈는 단체로 소풍을 떠났다. 즐거운 시간을 보내고 마지막에 단체사진을 찍기 위해 카메라 앞에 일렬로 나란히 섰다. 그런데 각자가 원하는 배치가 모두 programmers.co.kr 정답 class Solution { private int answer = 0; private String[] arr = {"A", "C", "F", "J", "M", "N", "R", "T"}; public int solution(int n, String[] data) { boolean[] v = new boolean[arr.length]; dfs("", v, data); return answer; } private void df..
-
LeetCode - 21. Merge Two Sorted Lists [Java], [Kotlin]Algorithms/- LeetCode 2022. 2. 5. 22:33
문제 링크 : 21. Merge Two Sorted Lists Merge Two Sorted Lists - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com # Java 정답(Solution) /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * L..
-
프로그래머스 - 오픈채팅방 [자바]Algorithms/- 프로그래머스 2022. 2. 4. 00:16
문제 링크 : 오픈채팅방 코딩테스트 연습 - 오픈채팅방 오픈채팅방 카카오톡 오픈채팅방에서는 친구가 아닌 사람들과 대화를 할 수 있는데, 본래 닉네임이 아닌 가상의 닉네임을 사용하여 채팅방에 들어갈 수 있다. 신입사원인 김크루는 카카오톡 오 programmers.co.kr 정답 import java.util.*; class Solution { public String[] solution(String[] record) { Map ssMap = new HashMap(); int cnt = 0; for(String str : record){ if(str.startsWith("Enter") || str.startsWith("Change")){ String[] arr = str.split(" "); ssMap.pu..
-
프로그래머스 - 정수 내림차순으로 배치하기 [자바]Algorithms/- 프로그래머스 2022. 2. 3. 23:41
문제 링크 : 정수 내림차순으로 배치하기 코딩테스트 연습 - 정수 내림차순으로 배치하기 함수 solution은 정수 n을 매개변수로 입력받습니다. n의 각 자릿수를 큰것부터 작은 순으로 정렬한 새로운 정수를 리턴해주세요. 예를들어 n이 118372면 873211을 리턴하면 됩니다. 제한 조건 n은 1이 programmers.co.kr 정답 import java.util.*; class Solution { public long solution(long n) { char[] arr = String.valueOf(n).toCharArray(); Arrays.sort(arr); return Long.valueOf(new StringBuilder(new String(arr)).reverse().toString()..
-
프로그래머스 - 두 정수 사이의 합 [자바]Algorithms/- 프로그래머스 2022. 2. 3. 23:24
문제 링크 : 두 정수 사이의 합 코딩테스트 연습 - 두 정수 사이의 합 두 정수 a, b가 주어졌을 때 a와 b 사이에 속한 모든 정수의 합을 리턴하는 함수, solution을 완성하세요. 예를 들어 a = 3, b = 5인 경우, 3 + 4 + 5 = 12이므로 12를 리턴합니다. 제한 조건 a와 b가 같은 경우 programmers.co.kr 정답 class Solution { public long solution(int a, int b) { if(a == b){ return a; }else{ int t = 0; if(b < a){ t = a; a = b; b = t; } int len = b - a; long res = 0; for(int i=0; i