본문 바로가기

dfs3

[leetcode] 784. Letter Case Permutation [leetcode] 784. Letter Case Permutation 문제 풀이 기본적으로 탐색하는 character가 문자면 소문자, 대문자로 변환하고 숫자면 다음 character로 넘어갔다. 마지막까지 완료하면 새로운 문자열을 반환하였다. 개념을 그림으로 표현해봤다. 주어진 문자열이 "a1b2" 이면 결과로 ["a1b2", "a1B2", "A1b2", "A1B2"] 가 반환된다. dfs 함수를 만들고 character가 문자면 두 갈래(소문자, 대문자)로 나눠서 계속 함수를 호출해 풀었다. 코드 class Solution { fun letterCasePermutation(s: String): List { val result = arrayListOf() dfs(s.toCharArray(), 0, r.. 2021. 10. 4.
[leetcode] 733. Flood Fill [leetcode] 733. Flood Fill 문제 풀이 주어진 2차원 배열에서 상하좌우 4방향으로 시작위치의 색과 같은 색이면 새로운 색으로 칠하면 되는 문제이다. dfs, bfs 풀이로 풀면 된다. 처음에 아래와 같이 작성했다가 Time Limit Exceeded 나왔다. class Solution { fun floodFill(image: Array, sr: Int, sc: Int, newColor: Int): Array { val baseColor = image[sr][sc] fun fill(row: Int, col: Int) { if (row !in image.indices || col !in image[0].indices || image[row][col] != baseColor) { retur.. 2021. 10. 3.
[leetcode] 111. Minimum Depth of Binary Tree [leetcode] 111. Minimum Depth of Binary Tree 문제 leetcode.com/problems/minimum-depth-of-binary-tree/ Minimum Depth of Binary Tree - 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 풀이 Root 노드에서 Leaf 노드로 가는 가장 짧은 거리를 찾는 문제이다. 문제자체는 BFS만 알면 풀 수 있는 수준이다.BFS(Breadth-first search) 이용해서 풀었.. 2021. 2. 28.