[LeetCode] 345. Reverse Vowels of a String
·
코딩테스트/Python
🔗 Problem Linkhttps://leetcode.com/problems/reverse-vowels-of-a-string/description/❔Thinking영단어가 주어지면, 모음(a,e,i,o,u)만을 역순으로 재배치하여 반환한다. (abe -> eba)알파벳은 소문자와 대문자를 구분한다.💻Solution1. 단어에 포함된 모음을 배열에 담고, 해당 배열에서 pop 하여 순서 재배치def reverseVowels(self, s: str) -> str: vowels_set = set(['a','e','i','o','u','A','E','I','O','U']) vowels_list = [] s = list(s) for char in s: if char in v..
[LeetCode] 605. Can Place Flowers
·
코딩테스트/Python
🔗 Problem Linkhttps://leetcode.com/problems/can-place-flowers/description❔Thinking0과 1로만 이루어진 flowerbed 배열이 주어질 때 주어진 꽃을 모두 심을 수 있다면 True, 아니라면 False를 반환한다.0인 곳에 꽃을 하나 심을 수 있고, 1이 인접하지 않아야 한다. (ex - [1,1], [0,1,1] 불가능)💻Solution1. 초기 풀이def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool: flowers = 0 if flowerbed == [0] or (flowerbed == [1] and n == 0): return True elif flow..
[LeetCode] 1768. Merge Strings Alternately
·
코딩테스트/Python
🔗 Problem Linkhttps://leetcode.com/problems/merge-strings-alternately/description/❔Thinkingword1, word2 두 단어가 주어질 때, 두 단어의 각 글자를 번갈아 합친 단어를 반환한다.길이가 다르다면, 남은 길이의 단어는 그대로 붙인다.Input: word1 = "abc", word2 = "pqr"Output: "apbqcr"Explanation: The merged string will be merged as so:word1: a b cword2: p q rmerged: a p b q c r💻Solution1. stack을 활용한 풀이class Solution: def mergeAlternately..
[Leetcode] 77. Combinations
·
코딩테스트/Python
🔗 Problem Link Combinations - 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 ❔Thinking 1부터 n까지의 숫자들 가운데 k개를 뽑아 조합한 결과를 반환한다. 💻Solution 1. 조합을 활용한 풀이 from itertools import combinations def combine(self, n: int, k: int): n_list = [i for i in range(1,n+1)] return list(combinations(n_..
[Leetcode] 46. Permutations
·
코딩테스트/Python
🔗 Problem Link Permutations - 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 ❔Thinking 주어진 숫자들을 모두 활용해 만들 수 있는 수열을 리스트에 담아 반환한다. 💻Solution 1. 조합을 활용한 풀이 from itertools import permutations def permute(self, nums): return list(permutations(nums, len(nums))) 2. DFS를 활용한 풀이 - 반복문 def ..
[Leetcode] 17. Letter Combinations of a Phone Number
·
코딩테스트/Python
🔗 Problem Link Letter Combinations of a Phone Number - 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 ❔Thinking 2에서 9까지 숫자가 주어질 때, 각 숫자에 적힌 알파벳으로 가능한 조합을 모두 담은 리스트를 반환한다. 💻Solution 1. 조합을 활용한 풀이 from itertools import product def letterCombinations(self, digits: str) -> List[str]: ..