Coding Test/LeetCode
-
[LeetCode] 116. Populating Next Right Pointers in Each Node c++Coding Test/LeetCode 2023. 1. 2. 23:12
문제 You are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following definition: next pointer 를 next right node를 가르키도록 다음 포인터를 채웁니다. next right node가 없으면 null pointer로 설정합니다. 문제 해결 Node* connect(Node* root) { //base case if (root == NULL) return NULL; //connects the left subtree of same level with right subtree of tha..
-
[LeetCode] 695. Max Area of Island c++Coding Test/LeetCode 2022. 12. 28. 21:25
문제 You are given an m x n binary matrix grid. An island is a group of 1's (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water. The area of an island is the number of cells with a value 1 in the island. Return the maximum area of an island in grid. If there is no island, return 0. 배열의 요소 값이 1이고, 1이 상하좌우로 연속되어 있을..
-
[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가 주어지면, 반복되는 문자가 없는 가장 긴 하위 문자열의 길이를 찾는..