본문 바로가기

전체 글

(133)
[LeetCode] Subsets(부분집합) https://leetcode.com/problems/subsets/description/ Subsets - LeetCode Can you solve this real interview question? Subsets - Given an integer array nums of unique elements, return all possible subsets (the power set). The solution set must not contain duplicate subsets. Return the solution in any order. Example 1: Input: n leetcode.com 문제 설명 부분집합을 구하는 문제이다. [1,2,3] 리스트가 주어졌을때 총 [],[1],[2],[3],[1,..
BFS(너비 우선 탐색) 스택을 이용하는 DFS와 달리 BFS는 반복 구조를 구현할 때 큐를 사용한다. 재귀로 구현 불가 큐를 이용하는 반복구현만 가능하다. graph = { 'A': ['B', 'C'], 'B': ['A', 'D'], 'C': ['A', 'G', 'H', 'I'], 'D': ['B', 'E', 'F'], 'E': ['D'], 'F': ['D'], 'G': ['C'], 'H': ['C'], 'I': ['C', 'J'], 'J': ['I'] } BFS 구현(큐 2개 사용) visited = deque() need_visited = deque() need_visited.append(list(graph.keys())[0]) 첫번째 키 값인 'A'를 먼저 방문이 필요한 큐에 넣어준다. while need_visited:..
[Python] Recursive(재귀함수) 1. 팩토리얼 구하기 재귀함수를 배울때 가장 대표적인 문제 def factorial(num:int): if num == 1: return num sum = num * factorial(num -1) return sum factorial(4) 실행 시 4* factorial(3) 호출 factorial(3) -> 3 * factorial(2) factorial(2) -> 2 * factorial(1) factorial(1) -> 1 반환 2 * 1 3 * (2 * 1) : 반환된 2 * 1 4 * 3 * 2 * 1 : 반환된 3 * 2 * 1
[LeetCode] 순열 https://leetcode.com/problems/permutations/ Permutations - LeetCode Can you solve this real interview question? Permutations - Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order. Example 1: Input: nums = [1,2,3] Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1], leetcode.com 문제해설: 사용자가 리스트를 입력하면 순열을 구하는 문제이다. [1,2,3]을 받았다면 이것을 이용해 [1,2,3..
[LeetCode] topKFrequent(상위 K 빈도 요소) https://leetcode.com/problems/top-k-frequent-elements/submissions/1079747150/ LeetCode - The World's Leading Online Programming Learning Platform 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번 이상 등장하는 갯수를 구한다. * 받아온 리스트의 문자들의 갯수를 써준다. * 딕셔너리의 value가 빈도수이기 때문에 value순으로 가장 ..
[LeetCode] numJewelsInStones(보석과 돌) https://leetcode.com/problems/jewels-and-stones/ Jewels and Stones - LeetCode Can you solve this real interview question? Jewels and Stones - You're given strings jewels representing the types of stones that are jewels, and stones representing the stones you have. Each character in stones is a type of stone you have. You want to kno leetcode.com 문제설명 문자열을 사용자가 입력하면 문자열(돌)에 얼마나 그 문자가 들어가있는지 확인하는 ..
[LeetCode] Longest Substring Without Repeating Characters( 중복 문자 없는 가장 긴 부분 문자열 ) https://leetcode.com/problems/longest-substring-without-repeating-characters/description/
[Python] 백준 17219. 비밀번호 찾기 https://www.acmicpc.net/problem/17219 17219번: 비밀번호 찾기 첫째 줄에 저장된 사이트 주소의 수 N(1 ≤ N ≤ 100,000)과 비밀번호를 찾으려는 사이트 주소의 수 M(1 ≤ M ≤ 100,000)이 주어진다. 두번째 줄부터 N개의 줄에 걸쳐 각 줄에 사이트 주소와 비밀번 www.acmicpc.net 문제가 직관적이라고 생각한다. 웹사이트에 맞는 비밀번호를 찾는 것이다. dict을 이용하면 쉽게 풀릴 문제라고 생각해서 dict으로 한 번 풀어본다. * 딕셔너리를 만들때 컬렉션에 나와있는 defaultdict 함수를 사용해보겠다. pwbook = collections.defaultdict(str) * 웹사이트와 비밀번호를 입력을 받을때 딕셔너리에 없다면 값을 추가해..