leetcode (25) 썸네일형 리스트형 [기타] 릿코드 Easy 20 'Valid Parentheses' (Python) https://leetcode.com/problems/valid-parentheses/ Valid Parentheses - 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 문자열에서 (), {}, []가 있는지 확인한다. (), {}, []가 있다면 문자열에서 없앤다. (), {}, []가 없을 때까지 반복한다. 문자열에 문자가 남아있다면 False를 반환하고, 없다면 True를 반환한다. class Solution: def isValid(self, s: str).. [기타] 릿코드 Easy 1816 'Truncate Sentence' (Python) https://leetcode.com/problems/truncate-sentence/ Truncate Sentence - 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 띄어쓰기로 문자열을 구분한다. 순서대로 k 수의 단어를 구한다. 단어별 띄어쓰기 구분으로 문자열을 만든다. class Solution: def truncateSentence(self, s: str, k: int) -> str: s = s.split(' ') return ' '.join(s[:k]) [DFS] 릿코드 200 Medium 'Number of Islands' (Python) https://leetcode.com/problems/number-of-islands/ Number of Islands - 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 1. 상하좌우로 1이 연결되어 있으면 하나의 섬이라고 본다. 2. DFS 알고리즘으로 해결한다. class Solution: def numIslands(self, grid: List[List[str]]) -> int: answer = 0 q = [] dx = [0,0,-1,1] dy = [-1,.. [해시] 릿코드 438 Medium 'Find All Anagrams in a String' (Python) https://leetcode.com/problems/find-all-anagrams-in-a-string/ Find All Anagrams in a String - 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 1. 순서 상관 없이 s 안에 연속으로 p의 문자열이 나열되어 있다면 첫번째 인덱스를 반환한다. 2. 순서가 상관이 없으므로 Counter 라이브러리 사용 3. 시간복잡도를 효율적으로 사용하기 위해 여러 변수를 선언해둠 from collections i.. [비트연산] 릿코드 231 Easy 'Power of Two' (Python) https://leetcode.com/problems/power-of-two/ Power of Two - 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 1. n이 2의 지수여야 하므로 n을 2진수로 바꾸면 모두 1이다 2. n에서 1을 빼면 10, 100000 처럼 나오므로 n-1과 n을 & 비트연산하면 무조건 0이 나올 것이다 3. n이 0보다 크고, n&(n-1)이 0이면 True가 나오게 한다. class Solution: def isPowerOfTwo(s.. [이분탐색] 릿코드 153 Medium 'Find Minimum in Rotated Sorted Array' (Python) https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/ Find Minimum in Rotated Sorted Array - 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 1. 배열의 최소 요소를 반환하라고 했으므로 min 함수 사용 class Solution: def findMin(self, nums: List[int]) -> int: return min(nums) [이분탐색] 릿코드 Medium 74 'Search a 2D Matrix' (Python) https://leetcode.com/problems/search-a-2d-matrix/ Search a 2D 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 1. 각 행에 target 값이 있으면 true class Solution: def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: for i in matrix: if target in i: return True retur.. [이분탐색] 릿코드 Medium 33 'Search in Rotated Sorted Array' (Python) https://leetcode.com/problems/search-in-rotated-sorted-array/ Search in Rotated Sorted Array - 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 1. nums에서 target의 인덱스 값을 찾아라 2. target 값이 없으면 -1 class Solution: def search(self, nums: List[int], target: int) -> int: if target in nums: .. 이전 1 2 3 4 다음