분류 전체보기(104)
-
[Python] zip()
Python 내장함수, zip(*iterables) zip(*iterables) zip()은 동일한 개수로 이루어진 자료형을 묶어주는 역할을 한다. 제너레이터로 리턴 되기 때문에, list나 dic 등 으로 변환 해 주어야 한다. list1 = [1,2,3] list2 = ["홍길동", "김철수", "박미애"] zip_list = zip(list1, list2) print(zip_list, type(zip_list)) # 제너레이터로 리턴 print(list(zip_list)) # 리스트로 변환 # dictionary로 변환 dic ={} for i, s in zip(list1, list2): dic[i] = s print(dic) """ [(1, '홍길동'), (2, '김철수'), (3, '박미애')]..
2021.01.26 -
[Leetcode] 347. Top K Frequent Elements
Leetcode 347. Top K Frequent Elements https://leetcode.com/problems/top-k-frequent-elements/ Top K Frequent Elements - 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 Solution 1 collections.Counter() 이용한 풀이. most_common()메소드를 사용했다. collections.Counter() import collections from typi..
2021.01.26 -
BOJ 1806. 부분합 (Python)
BOJ-1806. 부분합 https://www.acmicpc.net/problem/1806 1806번: 부분합 첫째 줄에 N (10 ≤ N < 100,000)과 S (0 < S ≤ 100,000,000)가 주어진다. 둘째 줄에는 수열이 주어진다. 수열의 각 원소는 공백으로 구분되어져 있으며, 10,000이하의 자연수이다. www.acmicpc.net Solution import sys N, S = map(int, input().split()) list = list(map(int, input().split())) sum_untill_n = [0] * (N+1) for i in range(1, N + 1): sum_untill_n[i] = sum_untill_n[i-1] + list[i-1] min_leng..
2021.01.25 -
[Leetcode] 771. Jewels and Stones
Leetcode 771. Jewels and Stones (3) Jewels and Stones - LeetCode Jewels and Stones - 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 Solution 1 리스트와 파이썬의 if x in y문법을 이용한 풀이. class Solution: def numJewelsInStones(self, jewels: str, stones: str) -> int: count = 0 J = ' '.join(jewels..
2021.01.19 -
[Leetcode] 706. Design HashMap
Leetcode 706.Design HashMap https://leetcode.com/problems/design-hashmap/ Design HashMap - 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 Solution 파이썬의 대표적인 해시형 자료형인 딕셔너리이용 class MyHashMap: def __init__(self): self.dict = {} def put(self, key: int, value: int) -> None: self.dict[k..
2021.01.19 -
[Python] heapq 모듈
heapq 모듈 힙 자료구조 heapq모듈은 이진 트리(binary tree)기반의 최소 힙(min heap)자료구조 제공. min heap을 사용하면 원소들이 항상 정렬된 상태로 삽입, 삭제되며 min heap에서 가장 작은 값은 언제나 인덱스 0, 즉 이진트리의 루트에 위치. heap 자료구조를 이용해 데이터를 정렬하려면 heap[0]를 루프를 돌아 heappop() 해주면 된다. 모듈 임포트 import heapq 최소 힙 (min heap) 생성 heap = [] 별개의 자료구조가 아닌 리스트를 힙처럼 다룰 수 있도록 하는 것 힙에 원소 추가 - heappush() heap모듈의 heappush()함수를 이용하여 원소를 추가 할 수 있다. 첫번째 인자는 원소를 추가할 대상 리스트이며 두번빼 인자는..
2021.01.19