전체 글
-
[LeetCode] 21. Merge Two Sorted Lists c++Coding Test/LeetCode 2023. 1. 4. 22:47
문제 You are given the heads of two sorted linked lists list1 and list2. Merge the two lists in a one sorted list. The list should be made by splicing together the nodes of the first two lists. Return the head of the merged linked list. 주어진 두 개의 노드를 오름차순으로 합치는 문제입니다. 문제 해결 RECURSIVE APPROACH class Solution { public: ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) { if(NULL == list1){ ret..
-
[LeetCode] 994. Rotting Oranges c++Coding Test/LeetCode 2023. 1. 4. 19:10
문제 You are given an m x n grid where each cell can have one of three values: 0 representing an empty cell, 1 representing a fresh orange, or 2 representing a rotten orange. Every minute, any fresh orange that is 4-directionally adjacent to a rotten orange becomes rotten. Return the minimum number of minutes that must elapse until no cell has a fresh orange. If this is impossible, return -1. 2는 썩..
-
[LeetCode] 542. 01 Matrix c++Coding Test/LeetCode 2023. 1. 3. 17:15
문제 Given an m x n binary matrix mat, return the distance of the nearest 0 for each cell. The distance between two adjacent cells is 1. 문제 해결 벡터값 초기화 값을 INT8_MAX로 하니 최대값이 127이라 fail이 나왔다. 1 = 0 && y res[curx][cury] + 1){ res[x][y] = res[curx][cury] + 1; q.push({x, y}); } } } } return res; } };
-
[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; ..