본문 바로가기

분류 전체보기267

[자료구조] Linked List 클래스 구현 class Node: def __init__(self, item): self.data = item self.next = None class LinkedList: def __init__(self): self.nodeCount = 0 self.head = Node(None) self.tail = None self.head.next = self.tail def traverse(self): result = [] curr = self.head while curr.next: curr = curr.next result.append(curr.data) return result def getAt(self, pos): if pos self.nodeCount: return None i = 0 curr.. 2021. 4. 20.
[자료구조] (singly) Linked List 파이썬 연결리스트를 끝까지 순회하는 방법 LeetCode 같은 경우 # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next def traverse(self): if self.head == None: return [] curr = self.head res = [] while curr: res.append(curr.data) curr = curr.next return res 2021. 4. 20.
[자료구조] 이진탐색 (Binary Search) 파이썬 (bisect, recursion, iteration) 일반적인 풀이방법 def solution(L, x): start, end = 0, len(L)-1 while start = low: mid = (high + low) // 2 # If element is present at the middle itself if arr[mid] == x: return mid # If element is smaller than mid, then it can only # be present in left subarray elif arr[mid] > x: return binary_search(arr, low, mid - 1, x) # Else the element can only be present in right subarray else: return binary_search.. 2021. 4. 20.
파이썬을 공부하는 방법 👍 좋은 글 내가 Python을 공부한 방법 내가 Python을 입문하고 공부한 방법을 소개합니다. shoark7.github.io 2021. 4. 20.
[자료구조] 재귀함수 파이썬 (recursive, iterative, decorator, reduce) Fibonacci 피보나치 (recursively & iteratively) 삼항 연산자(Ternary operators)로 풀이 (출처는 아래에) # basic recusrive solution def fibo(n): return fibo(n-1) + fibo(n-2) if n >= 2 else n # basic iterative solution def fibo(n): if n < 2: return n a, b = 0, 1 for i in range(n-1): a, b = b, a + b return b # iterative w/ DP def fibo(n): if n < 2: return n cache = [0 for _ in range(n+1)] cache[1] = 1 for i in range(2,.. 2021. 4. 20.
[List] append VS. extend s = list(range(3)) s.append(10) print(s) s = list(range(3)) s.append([10]) print(s) s = list(range(3)) s.extend([10]) print(s) output [0, 1, 2, 10] [0, 1, 2, [10]] [0, 1, 2, 10] 2021. 4. 19.
[LeetCode] Number Complement 파이썬 (format, bitwise) format() 함수를 사용해서 진법을 출력할때 앞의 str를 제거하고 출력할 수 있다. Format Specification Mini-Language 이런식으로 출력된다. print(format(num, 'b')) print(bin(num)) class Solution: def findComplement(self, num: int) -> int: com_bi = [str(abs(int(x)-1)) for x in str(format(num, 'b'))] return com_bi abs(int(x) - 1) 을 해줌으로써 1 -> 0 , 0 -> 1로 swap 해준다. 2021. 4. 19.
[LeetCode] Island Perimeter 파이썬 (island, traverse) 풀이방법: 2-d array를 traverse하면서 (row col) land 일 경우 4를 더해준다. 단, land일 경우 land의 위쪽과 왼쪽일 경우 -2를 더해준다. 왼쪽과 위쪽만 체크하는 이유는 traverse의 방향 때문에 그렇다. 그림으로 설명 오른쪽 아래 land 기준으로 위의 land에서 한개, 오른쪽 아래 land에서 한개 겹치는 land가 있을 때마다 2개씩 빼줘야 한다. 여기서는 grid의 side length를 1로 주어졌기 때문에 그냥 2를 빼주면 된다. class Solution(object): def islandPerimeter(self, grid): """ :type grid: List[List[int]] :rtype: int """ count, repeat = 0, 0 f.. 2021. 4. 19.
[LeetCode] Hamming Distance 파이썬 (bitwise) class Solution(object): def hammingDistance(self, x, y): """ :type x: int :type y: int :rtype: int """ distance = 0 z = x ^ y while z: distance += 1 z &= z - 1 return distance def hammingDistance2(self, x, y): """ :type x: int :type y: int :rtype: int """ return bin(x ^ y).count('1') 2번째 솔루션 비트 XOR 연산으로 position이 서로 다른지 확인한다. x = 1, y = 4일 경우 001 XOR 100 = 101 이 str에서 count메서드를 사용해서 1의 횟수를 retur.. 2021. 4. 19.