Algorithm
-
Linked List | Reverse Linked ListAlgorithm/Linked List 2021. 4. 29. 23:27
Given the head of a singly linked list, reverse the list, and return the reversed list. 말 그대로, head노드를 가지고 Linked List를 reverse해서, 새로운 head를 리턴하는 문제다. 모두 오른쪽을 바라보다가, 순차적으로 왼쪽을 바라보게 하는 그림을 연상시킨다. 이 문제를 한큐만에, O(N)으로 푸는 방법이 있다. 바로 지나가면서 뒤집는 방법이다. 코드는 다음과 같다. class Solution { public ListNode reverseList(ListNode head) { ListNode newNext = null; ListNode temp = null; ListNode curr = head; while(curr ..
-
Linked List | Delete Node in a Linked ListAlgorithm/Linked List 2021. 4. 28. 22:52
Write a function to delete a node in a singly-linked list. You will not be given access to the head of the list, instead you will be given access to the node to be deleted directly. It is guaranteed that the node to be deleted is not a tail node in the list. Constraints: * The number of the nodes in the given list is in the range [2, 1000]. * -1000
-
Linked List | Middle of the Linked ListAlgorithm/Linked List 2021. 4. 27. 23:16
Given a non-empty, singly linked list with head node head, return a middle node of linked list. If there are two middle nodes, return the second middle node. LinkedList에서 가운데에 있는 Node를 리턴하라는 문제이다. 처음 접근은 1.5N의 loop을 돌리는 방식이었다. class Solution { public ListNode middleNode(ListNode head) { int count = 0; ListNode curr = head; while(curr != null){ curr = curr.next; count++; } curr = head; count = co..
-
Linked List | Convert Binary Number in a Linked List to IntegerAlgorithm/Linked List 2021. 4. 26. 23:41
Given head which is a reference node to a singly-linked list. The value of each node in the linked list is either 0 or 1. The linked list holds the binary representation of a number. Return the decimal value of the number in the linked list. Linked List에 binary 값이 들어가있고 (0 또는 1), 그 값을 integer값으로 계산하는 조건의 문제이다. 처음에는 O(N)이지만, 실제로는 2N, 그러니까 Loop을 두번 도는 알고리즘을 구현했다. Linked List에서는 Easy 난이도의 문제이다. cla..
-
[JAVA/Algorithm] Programmers: 서울에서 김서방 찾기Algorithm/Programmers 2020. 11. 17. 22:24
String형 배열 seoul의 element중 Kim의 위치 x를 찾아, 김서방은 x에 있다는 String을 반환하는 함수, solution을 완성하세요. seoul에 Kim은 오직 한 번만 나타나며 잘못된 값이 입력되는 경우는 없습니다.제한 사항 seoul은 길이 1 이상, 1000 이하인 배열입니다. seoul의 원소는 길이 1 이상, 20 이하인 문자열입니다. Kim은 반드시 seoul 안에 포함되어 있습니다. 입출력 예 seoul return [Jane, Kim] 김서방은 1에 있다 해법 1: 가장 기본적인 해법이다. loop을 이용해 Kim 이 나올때까지 찾은 후, 그 index 를 프린트해준다. class Solution { public String solution(String[] seoul..
-
[JAVA/Algorithm] Programmers: 문자열 내림차순으로 배치하기Algorithm/Programmers 2020. 11. 17. 22:00
문자열 s에 나타나는 문자를 큰것부터 작은 순으로 정렬해 새로운 문자열을 리턴하는 함수, solution을 완성해주세요. s는 영문 대소문자로만 구성되어 있으며, 대문자는 소문자보다 작은 것으로 간주합니다.제한 사항 str은 길이 1 이상인 문자열입니다. 입출력 예 s return Zbcdefg gfedcbZ 해법: split 을 이용해 풀었다. 그 후 sort 와 reverse를 한 후 join을 한다. String의 여러 function 들을 이용해봤다. import java.util.*; class Solution { public String solution(String s) { String[] char_list = s.split(""); Arrays.sort(char_list); Collectio..
-
[JAVA/Algorithm] Programmers: 문자열 내 p와 y의 개수Algorithm/Programmers 2020. 11. 17. 21:43
대문자와 소문자가 섞여있는 문자열 s가 주어집니다. s에 'p'의 개수와 'y'의 개수를 비교해 같으면 True, 다르면 False를 return 하는 solution를 완성하세요. 'p', 'y' 모두 하나도 없는 경우는 항상 True를 리턴합니다. 단, 개수를 비교할 때 대문자와 소문자는 구별하지 않습니다. 예를 들어 s가 pPoooyY면 true를 return하고 Pyy라면 false를 return합니다. 제한사항 문자열 s의 길이 : 50 이하의 자연수 문자열 s는 알파벳으로만 이루어져 있습니다. 입출력 예 s answer pPoooyY true Pyy false 입출력 예 설명 입출력 예 #1 'p'의 개수 2개, 'y'의 개수 2개로 같으므로 true를 return 합니다. 입출력 예 #2 '..
-
[JAVA/Algorithm] Programmers: 주식가격Algorithm/Programmers 2020. 11. 17. 15:59
level 2 문제 설명 초 단위로 기록된 주식가격이 담긴 배열 prices가 매개변수로 주어질 때, 가격이 떨어지지 않은 기간은 몇 초인지를 return 하도록 solution 함수를 완성하세요. 제한사항 prices의 각 가격은 1 이상 10,000 이하인 자연수입니다. prices의 길이는 2 이상 100,000 이하입니다. 입출력 예 prices return [1, 2, 3, 2, 3] [4, 3, 1, 1, 0] 입출력 예 설명 1초 시점의 ₩1은 끝까지 가격이 떨어지지 않았습니다. 2초 시점의 ₩2은 끝까지 가격이 떨어지지 않았습니다. 3초 시점의 ₩3은 1초뒤에 가격이 떨어집니다. 따라서 1초간 가격이 떨어지지 않은 것으로 봅니다. 4초 시점의 ₩2은 1초간 가격이 떨어지지 않았습니다. 5초..