[HackerRank] Solve Me First
2021. 9. 11. 03:36
알고리즘
# Solve Me First (Warmup) [문제] [코드] int solveMeFirst(int a, int b) { return a+b; } [코드설명] 두 정수의 합을 구하는 문제다. 변수를 따로 생성해서 풀지 않고, a와 b를 더한 값을 바로 반환했다. 이때 a와 b모두 int타입이므로 반환 타입도 int가 된다. [채점 결과]
[HackerRank] Balanced Brackets
2021. 9. 11. 03:30
알고리즘
# Balanced Brackets (Stacks) [문제] [코드] string isBalanced(string s) { stack stack; string result; int i; for(i = 0; i < s.size(); i++) { char c; c = s.at(i); if(c=='(' || c=='{' || c=='[') stack.push(c); else { if(!stack.empty() && ((stack.top()=='(' && c==')') || (stack.top()=='{' && c=='}') || (stack.top()=='[' && c==']'))) stack.pop(); else return "NO"; } } return result = stack.empty() ? "YES..
[HackerRank] Correctness and the Loop Invariant / Running Time of Algorithms
2021. 8. 28. 19:37
알고리즘
# Correctness and the Loop Invariant (Sorting) [문제] [코드] - 원래 코드 void insertionSort(int N, int arr[]) { int i,j; int value; for(i=1;i0 && value
[HackerRank] Binary Search Tree : Lowest Common Ancestor / Insertion Sort - Part 2
2021. 8. 28. 16:45
알고리즘
# Binary Search Tree : Lowest Common Ancestor (Trees) [문제] [코드] Node *lca(Node *root, int v1,int v2) { // Write your code here. int min_v = min(v1, v2); int max_v = max(v1, v2); while(root) { int cur = root->data; // left subtree if (cur > max_v) root = root->left; // right subtree else if (cur right; // LCA else return root; } return root; } [코드설명] LCA(Lowest Common Ancest..
[HackerRank] Inserting a Node Into a Sorted Doubly Linked List / Delete duplicate-value nodes from a sorted linked list
2021. 8. 16. 19:40
알고리즘
# Inserting a Node Into a Sorted Doubly Linked List (Linked Lists) [문제] [코드] DoublyLinkedListNode* sortedInsert(DoublyLinkedListNode* llist, int data) { DoublyLinkedListNode *node = new DoublyLinkedListNode(data); DoublyLinkedListNode *temp = llist; DoublyLinkedListNode *prev; if(llist == NULL) return node; if(data data) { node->next = temp; temp->prev = node; llist = node; } else { while(temp =..
[HackerRank] Find Merge Point of Two Lists / Reverse a doubly linked list
2021. 8. 9. 14:56
알고리즘
# Find Merge Point of Two Lists (Linked Lists) [문제] [코드] int findMergeNode(SinglyLinkedListNode* head1, SinglyLinkedListNode* head2) { SinglyLinkedListNode* temp1 = head1, *temp2 = head2; while(temp1 != temp2) { temp1 = (temp1 == NULL) ? head2 : temp1->next; temp2 = (temp2 == NULL) ? head1 : temp2->next; } return temp1->data; } [코드설명] 각 포인터를 동시에 한 단계씩 이동시키면 둘 사이의 간격은 리스트의 크기 차이만큼 난다. 이때 먼저 NULL에..
[HackerRank] Merge two sorted linked lists / Get Node Value
2021. 7. 19. 16:29
알고리즘
# Merge two sorted linked lists (Linked Lists) [문제] [코드] /* class SinglyLinkedListNode { public: int data; SinglyLinkedListNode *next; SinglyLinkedListNode(int node_data) { this->data = node_data; this->next = nullptr; } }; class SinglyLinkedList { public: SinglyLinkedListNode *head; SinglyLinkedListNode *tail; SinglyLinkedList() { this->head = nullptr; this->tail = nullptr; } void insert_node(i..
[HackerRank] Insert a node at a specific position in a linked list / Compare two linked lists
2021. 7. 19. 14:35
알고리즘
# Insert a node at a specific position in a linked list (Linked Lists) [문제] [코드] SinglyLinkedListNode* insertNodeAtPosition(SinglyLinkedListNode* llist, int data, int position) { SinglyLinkedListNode *node = new SinglyLinkedListNode(data); if(position == 0) { node->next = llist; llist = node; } int i = 0; SinglyLinkedListNode *temp = llist; while(i != (position-1)) { temp = temp->next; i++; } no..
[HackerRank] Print in Reverse / Delete a Node
2021. 7. 18. 02:56
알고리즘
# Print in Reverse (Linked Lists) [문제] [코드] void reversePrint(SinglyLinkedListNode* llist) { SinglyLinkedListNode *two = NULL, *three, *temp; if(!llist) return; do{ three = two; two = llist; llist = llist->next; two->next = three; } while(llist); for(temp = two; temp; temp = temp->next) cout data next가 three의 값을 가지게 하여 반대로 연결했다. 그리고 llist가 NULL인지 확인하고 해당 과정을 반복하게 된다. llist가 NULL이면 그 다음으로 더이상 노드가..