Algorithm
-
[LeetCode] 848. Shifting Letters 문제풀이Algorithm/문제풀이 2021. 9. 8. 16:41
https://leetcode.com/problems/shifting-letters/ Shifting Letters - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 문제 내용 문자를 shift 한 결과를 출력하는 문제 문제 풀이 어차피 다 shift 한 결과를 구하는 것이기 때문에 문자를 다 'a' 로 초기화 되있다고 생각하고 shift 값만 구하면 된다. class Solution { public: string shiftingLetters(string s, ..
-
[LeetCode] 206. Reverse Linked List 문제 풀이Algorithm/문제풀이 2021. 9. 8. 16:04
https://leetcode.com/problems/reverse-linked-list/ Reverse Linked List - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 문제 내용 링크드 리스트를 거꾸로 출력하면 된다. 문제 풀이 3개의 변수를 두고 노드 방향을 뒤집고 한칸씩 땡기고 했다. class Solution { public: ListNode* reverseList(ListNode* head) { if (head == nullptr) return ..
-
[LeetCode] 994. Rotting Oranges 문제 풀이Algorithm/문제풀이 2021. 9. 8. 16:01
https://leetcode.com/problems/rotting-oranges/ Rotting Oranges - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 문제 내용 썩은 귤이 있는데 4방향으로 인접해 있는 곳으로 퍼진다. 몇일 만에 남은 귤이 다 썩는지 출력하고 다 안썩으면 -1 출력하는 문제 문제 풀이 썩은 귤을 모두 Queue에 담고 하나씩 퍼져가면서 day를 구하면 된다. 그리고 마지막에 귤을 검사해서 멀쩡한 귤이 있으면 -1을 출력하면 된다. c..
-
[LeetCode] 542. 01 Matrix 문제 풀이Algorithm/문제풀이 2021. 9. 8. 15:57
https://leetcode.com/problems/01-matrix/ 01 Matrix - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 문제 내용 2차원 배열에서 1값에서 가장 가까운 0까지의 거리를 모두 구하는 문제 문제 풀이 처음에는 모든 점을 탐색하면서 1을 찾고 0으로 BFS를 돌리는 방법을 생각했는데 시간초과가 걸린다. 가만히 생각하면 BFS 1번만 돌리면 모든 점의 거리를 구할 수 있다. 0인 지점을 미리 queue에 다 담아서 최단거리를 각각 ..
-
[LeetCode] 116. Populating Next Right Pointers in Each Node 문제풀이Algorithm/문제풀이 2021. 9. 6. 21:31
https://leetcode.com/problems/populating-next-right-pointers-in-each-node/ Populating Next Right Pointers in Each Node - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 문제 내용 완전 바이너리 트리가 주어지는데 같은 depth를 왼쪽에서 오른쪽으로 연결된 링크드 리스트 처럼 연결시키는 문제 문제 풀이 Depth마다 연결하는 문제로 BFS를 이용하기에 딱이다. Depth..
-
[LeetCode] 617. Merge Two Binary Trees 문제풀이Algorithm/문제풀이 2021. 9. 6. 21:28
https://leetcode.com/problems/merge-two-binary-trees/ Merge Two Binary Trees - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 문제 내용 2개의 바이너리 트리가 주어지는데 2개를 합친 트리의 root를 return 하면 된다. 합칠 때는 규칙이 있는데, 한쪽이 nullptr 이면 덮어 쓰고, 둘다 노드가 존재하면 sum을 해서 값을 갱신하면 된다. 문제 풀이 조건에 맞춰서 재귀함수로 구현하면 깔끔하게 ..
-
[LeetCode] 695. Max Area of Island 문제풀이Algorithm/문제풀이 2021. 9. 6. 21:25
https://leetcode.com/problems/max-area-of-island/ Max Area of Island - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 문제 내용 grid 맵에 1로 표시된 island가 있는데 가장 큰 영역 찾기 문제 풀이 BFS써서 각 섬에서 영역을 구하고, 가장 큰 영역을 찾으면 된다. class Solution { public: int row,col; int dy[4] = {-1, 0, 1, 0}; int dx[4] ..
-
[LeetCode] 733. Flood Fill 문제풀이Algorithm/문제풀이 2021. 9. 6. 21:23
https://leetcode.com/problems/flood-fill/ Flood Fill - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 문제 내용 특정 지점에서 상하좌우 4방향으로 움직이면서 같은 색을 모두 칠하는 문제 문제 풀이 bfs를 이용해서 하나씩 색칠하면 된다. color가 같거나 target color가 아니면 skip 해야하는 조건이 필요하다. class Solution { public: vector floodFill(vector& imag..