반응형
반응형
문제 풀이 기본적으로 탐색하는 character가 문자면 소문자, 대문자로 변환하고 숫자면 다음 character로 넘어갔다. 마지막까지 완료하면 새로운 문자열을 반환하였다. 개념을 그림으로 표현해봤다. 주어진 문자열이 "a1b2" 이면 결과로 ["a1b2", "a1B2", "A1b2", "A1B2"] 가 반환된다. dfs 함수를 만들고 character가 문자면 두 갈래(소문자, 대문자)로 나눠서 계속 함수를 호출해 풀었다. 코드 class Solution { fun letterCasePermutation(s: String): List { val result = arrayListOf() dfs(s.toCharArray(), 0, result) return result } fun dfs(target: C..
문제 풀이 a + b + c = 0 인 경우를 찾는 문제이다. 투 포인터를 이용해서 풀었다. 우선 정렬을 한 다음에 for 루프를 돌면서 nums[index] 가 a 라고 할 때 포인터를 이동하면서 b 와 c 값을 찾았다. 문제에서 답에 중복을 허용하지 않기 때문에 중복인 경우는 다시 계산하지 않게 처리했다. [0, 0, 0, 0, 0, 0] 와 같은 인풋이 들어왔을 때 한 번만 계산하고 스킵하게 된다. 시간복잡도는 O(N^2) 이 된다. => 정렬하는 데 O(NlogN) + for 루프 돌면서 b와 c값 찾는 데 O(N^2) 코드 fun threeSum(nums: IntArray): List { nums.sort() val result: MutableList = mutableListOf() for (i..
문제 Given an array of strings, group anagrams together. Example: Input: ["eat", "tea", "tan", "ate", "nat", "bat"], Output: [ ["ate","eat","tea"], ["nat","tan"], ["bat"] ] Note: All inputs will be in lowercase. The order of your output does not matter. 풀이 Anagram 은 단어나 문장을 구성하고 있는 문자의 순서를 바꾸어 다른 단어나 문장을 만드는 놀이이다. 주어진 input에서 Anagram 인 것들만 모아주면 되는 문제이다. input이 모두 소문자이기 떄문에 대소문자를 구분할 필요가 없다. map를 이..
문제 Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted in ascending from left to right. Integers in each column are sorted in ascending from top to bottom. Example : Consider the following matrix: ] Given target = 5, return true. Given target = 20, return false. 풀이 주어진 matrix에서 target 값이 존재하는 지를 찾는 문..