알고리즘, 자료구조 9

[알고리즘] Leetcode. Remove Nth Node From End of List

Given the head of a linked list, remove the nth node from the end of the list and return its head. 연결리스트 head과 n이 주어지고 끝에서 n번째 노드를 삭제하고 head를 return 하라 Example 1: 방법 1. 처음 생각한 방법 2개의 포인터가 필요함. p1(포인터1)은 head부터 시작, p2는 head.next부터 시작 리스트 길이를 계산하고 (리스트 길이 - n) 만큼 이동해 p1.next는 지울 노드가 되고 p2는 지울 노드를 가리킨다. 고려해야 할 코너케이스 1. n이 length와 같을 때 : 이 경우에는 p1 head 자체를 지워야한다. 2. length가 1일 때 : 위 첫 번째 케이스와 같은 케이스..

[알고리즘] Leetcode. Linked List Cycle 4개의 해결 방법

Given head, the head of a linked list, determine if the linked list has a cycle in it. There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail's next pointer is connected to. Note that pos is not passed as a parameter. Return true if there is a cycle..

[알고리즘] Leetcode. Design Linked List

MyLinkedList() Initializes the MyLinkedList object. int get(int index) Get the value of the indexth node in the linked list. If the index is invalid, return -1. 인덱스가 맞지 않으면 -1을 리턴 void addAtHead(int val) Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. void addAtTail(int val) Append a node of value v..

[알고리즘] Leetcode. Replace Elements with Greatest Element on Right Side

Given an array arr, replace every element in that array with the greatest element among the elements to its right, and replace the last element with -1. After doing so, return the array. 배열 arr가 주어지고 모든 엘리먼트를 오른쪽에 있는 엘리먼트 중 제일 큰 엘리먼트로 바꾸세요. 그리고 마지막 엘리먼트는 -1로 바꾸세요. 마지막으로 배열을 리턴하세요. - 처음 푼 코드 var replaceElements = function(arr) { let greatestNumber; for (let i = 0; i < arr.length; i++) { greatestN..

[알고리즘] Leetcode. Find Numbers with Even Number of Digits

Given an array nums of integers, return how many of them contain an even number of digits. 정수가 담겨있는 배열이 주어지고, 짝수 자릿수가 몇 개인지 리턴하세요. Example 1: Input: nums = [12,345,2,6,7896] Output: 2 Explanation: 12 contains 2 digits (even number of digits). 345 contains 3 digits (odd number of digits). 2 contains 1 digit (odd number of digits). 6 contains 1 digit (odd number of digits). 7896 contains 4 digits ..

[알고리즘] Leetcode 704. Binary Search 이진 검색 알고리즘

704. Binary Search Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1. You must write an algorithm with O(log n) runtime complexity. 오름차순으로 주어진 정수 배열과 정수 타겟이 주어지고 타겟을 숫자로 검색하는 함수를 만드세요. 타겟이 존재한다면 index를 리턴하고 그렇지 않으면 -1를 리턴하세요. 시간 복잡도는 반드시 O(log n)이어야 합니..

[알고리즘] random6 함수를 가지고 사칙연산만 사용해서 random12 함수 만들기

랜덤하게 1에서 6으로 나오는 함수를 가지고 if문, 사칙연산만 사용해서 1 ~ 12 까지의 랜덤숫자가 나오는 random12함수 만들기 function random6() { return Math.ceil(Math.random() * 6); } function random12() { if (random6() % 2) { return random6() + 6; } else { return random6(); } } 1 ~ 6까지의 숫자는 확보가 되어있고 7 ~ 12까지의 숫자만 더 나오게 해주면 된다. 12까지의 랜덤숫자가 나오는 함수는 1 ~ 6 숫자가 나오는 확률 50%, 7 ~ 12숫자가 나오는 확률 50% 가지게 된다. 그렇다면 random6 함수로 확률을 나눌 수 있는 방법을 찾으면 될거라 생각했..