Coding Test
-
[LeetCode] 3. Longest Substring Without Repeating Characters c++Coding Test/LeetCode 2022. 12. 27. 15:53
문제 Given a string s, find the length of the longest substring without repeating characters. Input: s = "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3. Input: s = "pwwkew" Output: 3 Explanation: The answer is "wke", with the length of 3. Notice that the answer must be a substring, "pwke" is a subsequence and not a substring. 문자 s가 주어지면, 반복되는 문자가 없는 가장 긴 하위 문자열의 길이를 찾는..
-
[LeetCode] 19. Remove Nth Node From End of List c++Coding Test/LeetCode 2022. 12. 27. 13:58
문제 Given the head of a linked list, remove the nth node from the end of the list and return its head. linked list에 뒤에서 n번째 노드를 제거하는 문제입니다. 문제 해결 class Solution { public: ListNode* removeNthFromEnd(ListNode* head, int n) { ListNode *fast = head, *slow = head; while(n--){ fast = fast->next; } if(!fast->next){ return head->next; } while(fast->next){ fast = fast->next; slow = slow->next; } slow->nex..
-
[LeetCode] 876. Middle of the Linked List c++Coding Test/LeetCode 2022. 12. 26. 18:45
문제 Given the head of a singly linked list, return the middle node of the linked list. If there are two middle nodes, return the second middle node. 단일 링크로 이어져 있는 리스트에서 중간에 있는 노드를 구하는 문제입니다. 문제 해결 class Solution { public: ListNode* middleNode(ListNode* head) { ListNode *fast = head, *slow = head; while(fast){ fast = fast->next; if(fast){ fast = fast->next; } else{ break; } slow = slow->next; } re..
-
[LeetCode] 557. Reverse Words in a String III c++Coding Test/LeetCode 2022. 12. 26. 17:59
문제 Given a string s, reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order. Input: s = "Let's take LeetCode contest" Output: "s'teL ekat edoCteeL tsetnoc" 문장에 있는 각 단어의 순서를 반대로 바꾸는 문제입니다. 문제 해결 class Solution { private: void parseString(string& str, vector& vec, const string& delimiter){ int pos = 0; while( (pos = str.find(delimite..
-
[LeetCode] 344. Reverse String c++Coding Test/LeetCode 2022. 12. 26. 16:49
문제 Write a function that reverses a string. The input string is given as an array of characters s. You must do this by modifying the input array in-place with O(1) extra memory. Input: s = ["h","e","l","l","o"] Output: ["o","l","l","e","h"] input으로 주어지는 벡터 s의 요소들의 순서를 반대로 뒤집는 문제입니다. 문제 해결 class Solution { public: void reverseString(vector& s) { reverse(s.begin(), s.end()); } }; 다른 풀이 class Solut..
-
[LeetCode] 167. Two Sum II - Input Array Is Sorted c++Coding Test/LeetCode 2022. 12. 26. 16:18
문제 Given a 1-indexed array of integers numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number. Let these two numbers be numbers[index1] and numbers[index2] where 1
-
[LeetCode] 283. Move Zeroes c++Coding Test/LeetCode 2022. 12. 24. 17:54
문제 Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero elements. Note that you must do this in-place without making a copy of the array. Input: nums = [0,1,0,3,12] Output: [1,3,12,0,0] 문제 해결 처음에는 벡터 원소에 0이 있으면 삭제하는 문제인 줄 알았다. remove_if() 함수를 사용해서 0이라면 삭제하는 코드를 작성했다. class Solution { public: static bool oper(const int& a){ if( a == 0){ r..
-
[LeetCode] 606. Construct String from Binary Tree c++Coding Test/LeetCode 2022. 12. 24. 14:58
문제 Given the root of a binary tree, construct a string consisting of parenthesis and integers from a binary tree with the preorder traversal way, and return it. Omit all the empty parenthesis pairs that do not affect the one-to-one mapping relationship between the string and the original binary tree. Input: root = [1,2,3,null,4] Output: "1(2()(4))(3)" Explanation: Almost the same as the first ex..