컴퓨터공학 83

[Python3] 다차원 리스트 생성

계속 까먹는 파이썬 다차원 리스트 생성, 알고리즘 문제 푸는 데 유용한 도구가 되니 더 이상 까먹지 않도록 블로그에 기록합니다. 생성 모든 원소 값 0을 갖는 2차원 리스트를 생성합니다. n = 2 table = [[0 for _ in range(n)] for _ in range(n)] # [[0,0], [0,0]] 주의점 아래 프로그램을 실행하여 동일한 결과를 얻을 수 있으나, 일반적인 다차원 리스트를 생성하는 것이 아닌 각 인덱스에 대한 참조 리스트를 생성합니다. n = 2 table =[([0]*n)] *n # [[0,0], [0,0]] table[1][1] 값을 1로 만들고자 할 때, table[0][1] 또한 1로 변경되는 것을 확인할 수 있습니다. print(table) # [[0,0], [0..

[LeetCode] 167. Two Sum II - Input Array Is Sorted

Two pointer 알고리즘을 활용하여 시간복잡도가 O(n) 이내가 되도록 문제를 풀어야 합니다. 본문 https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/ Two Sum II - Input Array Is Sorted - LeetCode Two Sum II - Input Array Is Sorted - Given a 1-indexed array of integers numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number. Let these two numbers be numbers..

[LeetCode] 438. Find All Anagrams in a String

Sliding Window 알고리즘과 HashMap (파이썬에서는 dictionary로 구현) 자료구조에 대한 이해가 필요한 문제였습니다. https://leetcode.com/problems/find-all-anagrams-in-a-string 문제 핵심 일반적인 반복문 또는 Two pointer 기법을 사용하면 time limit 따라서 계산 횟수를 최소화 하는 기법을 사용해야 함 Two pointer 알고리즘과 비슷한 Sliding Window 알고리즘을 사용해야 한다 두 개의 해쉬맵을 만들어 계산을 간소화 솔루션 class Solution: def findAnagrams(self, s: str, p: str) -> List[int]: s_length, p_length = len(s), len(p)..