본문 바로가기

분류 전체보기24

[leetcode] combinations combinations n, k ( n >= k >= 1) 의 정수가 주어질때 n 개의 정수 (1 ~ n) 에서 k 개의 수를 뽑는 조합을 리턴 Solution1 itertools의 combinations 이용 code1 from itertools import combinations class Solution(object): def combine(self, n, k): return list(map(list,combinations(range(1,n+1),k))) Solution2 dfs(selected:list,start:int) selected에 el 이 추가된 경우 다음의 dfs에서 start = el + 1 종료조건 len(selected) == k code2 class Solution(object).. 2022. 1. 13.
[leetcode] permutations permutations 서로다른 숫자로 구성된 배열을 입력으로 받는다 가능한 모든 조합의 순열을 리턴 Solution1 itertools의 permutations를 사용 code1 from itertools import permutations class Solution(object): def permute(self, nums): return list(map(list,permutations(nums))) Solution2 next_permutation(nums)메서드 구현 nums[i-1] < nums[i] 인 가장 큰 i 찾기 nums[i:] 범위에서 nums[i-1] < nums[j] 인 가장큰 j 찾기 nums[i-1] 와 nums[j] 를 swap nums[i:] 를 양 끝에서 가운데 방향으로 sw.. 2022. 1. 13.
[leetcode] letter combinations of a phone number letter combinations of a phone number "2~9" 의 8가지 숫자의 조합으로 된 문자열을 입력으로 각 숫자는, 문자열 집합의 문자들 중 하나의 문자로 대응될수 있음 숫자열이 주어졌을때 조합가능한 모든 문자열 리턴 Solution 1 itertools의 product 이용 곱집합 code 1 from itertools import product class Solution(object): def letterCombinations(self, digits): """ :type digits: str :rtype: List[str] """ book = { &#39;2&#39;:[&#39;a&#39;,&#39;b&#39;,&#39;c&#39;], &#39;3&#39;:[&#39;d&#39.. 2022. 1. 13.
number of islands number of islands m x n 의 2차원 그리드 "1" 은 land,"0"은 water를 의미 그리드 공간에서 islands(연결된 "1")의 개수 반환 solution dfs를 이용 m x n 의 각 점들중 "1"인점 을 출발점으로 해서 도달가능한 모든점을 방문(dfs) 하고, 섬의개수 1증가 시키기 한번 방문한 점은 "0" 으로 표시 grid 상에서 한 점(i,j)의 방문가능한 이웃한 점은 상하좌우에 위치하면서, 그리드 범위 이내이고 값이 "1" 인점 code class Solution(object): def numIslands(self, grid): """ :type grid: List[List[str]] :rtype: int """ # m , n , dirs 초기화 m, n = len.. 2022. 1. 13.