Coding Test/LeetCode
-
[LeetCode] 733. Flood Fill c++Coding Test/LeetCode 2022. 12. 28. 20:45
문제 An image is represented by an m x n integer grid image where image[i][j] represents the pixel value of the image. You are also given three integers sr, sc, and color. You should perform a flood fill on the image starting from the pixel image[sr][sc]. To perform a flood fill, consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color as the s..
-
[LeetCode] 1876. Substrings of Size Three with Distinct Characters c++Coding Test/LeetCode 2022. 12. 27. 20:03
문제 A string is good if there are no repeated characters. Given a string s, return the number of good substrings of length three in s. Note that if there are multiple occurrences of the same substring, every occurrence should be counted. A substring is a contiguous sequence of characters in a string. Input: s = "xyzzaz" Output: 1 Explanation: There are 4 substrings of size 3: "xyz", "y..
-
[LeetCode] 567. Permutation in String c++Coding Test/LeetCode 2022. 12. 27. 18:00
문제 Given two strings s1 and s2, return true if s2 contains a permutation of s1, or false otherwise. In other words, return true if one of s1's permutations is the substring of s2. Input: s1 = "ab", s2 = "eidbaooo" Output: true Explanation: s2 contains one permutation of s1 ("ba"). 문제 해결 class Solution { public: bool checkInclusion(string s1, string s2) { if( s1.size() > s2.size()) return false; ..
-
[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..