본문 바로가기

프로그래머

[Git] cheat sheet 모음 | git 명령어 정리 git 명령어 정리 git init git status git add chapter1.txt git commit -m "Complete Chapter 1" git log git diff chapter3.txt git checkout chapter3.txt roll back to the last version committed in our local repo git remote add origin "url of the remote repo on Git" origin is the name of the remote git push -u origin master u flag which links up the remote and the local repo origin : name of remote master :.. 더보기
[Python] Leet Code 206. Reverse Linked List 본 내용은 를 참고했습니다. 자료형 변환 # Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution: def reverseList(self, lst: ListNode) -> ListNode: node, prev = lst, None while node: next, node.next = node.next, prev prev, node = node, next return prev def toList(self, node: ListNode) -> List: list: List = [] while node: lis.. 더보기
[Python] Leet Code 206. Reverse Linked List 본 내용은 를 참고했습니다. 재귀 구조로 뒤집기 # Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution: def reverseList(self, head: ListNode) -> ListNode: def reverse(node: ListNode, prev: ListNode = None): if not node: return prev next, node.next = node.next, prev return reverse(next, node) return reverse(head) 이 문제에서 배워야 할 포.. 더보기
[개체지향 프로그래밍] 상속 상속 상속(inheritance) 거의 모든 사람이 OOP의 핵심이라 여기는 특성 초창기 OO에서 가장 중요한 특성이라 여김 현재에도 상속을 지원하지 않으면 OO 언어라고 안 보는 게 보통 OOP의 또 다른 매우 중요한 특성인 다형성의 기반 OOP에서의 상속이란? 이미 존재하는 클래스를 기반으로 새 클래스를 만드는 방법 새 클래스는 기존 클래스의 동작과 상태를 그대로 물려 받음(유전) 그 외에 새 클래스만의 동작과 상태를 추가 가능(진화) 물론 이 새 클래스를 상속해서 또 다른 클래스를 만들 수 있음 이미 존재하는 클래스를 부르는 이름 부모(parent) 클래스 기반(base) 클래스 새 클래스를 부르는 이름 자식(child) 클래스 파생(derived) 클래스 두 클래스 간의 상속 관게를 설명하는 표현.. 더보기
[Python] Leet Code 21. Merge Two Sorted Lists 본 내용은 를 참고했습니다. 리스트 변환 class Solution: def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode: if (not l1) or (l2 and l2.val < l1.val): l1, l2 = l2, l1 if l1: l1.next = self.mergeTwoLists(l1.next, l2) return l1 이 문제에서 배워야할 포인트 재귀활용!! python의 swap 작은 값을 무조건 l1에 더보기
[Python] Leet Code 234. Palindrome Linked List 본 내용은 를 참고했습니다. 리스트 변환 class Solution: def isPalindrome(self, head: ListNode) -> bool: q: List = [] if not head: return True node = head while node: q.append(node.val) node = node.next while len(q) > 1: if q.pop(0) != q.pop(): return False return True 이 문제에서 배워야할 포인트 일반 list에서의 popleft : .pop(0) 느림 데크를 이용한 최적화 class Solution: def isPalindrome(self, head: ListNode) -> bool: q: Deque = collections.. 더보기
[Python] Leet Code 121. Best Time to Buy and Sell Stock 본 내용은 를 참고했습니다. 저점과 현재 값과의 차이 계산 class Solution: def maxProfit(self, prices: List[int]) -> int: price_min = sys.maxsize profit = 0 for price in prices: price_min = min(price_min, price) profit = max(profit, price - price_min) return profit 이 문제에서 배워야할 포인트 파이썬에서 시스템상 최댓값 설정 sys.maxsize float(inf) min, max 내장함수 이용 더보기
[Python] Leet Code 238. Product of Array Except Self 본 내용은 를 참고했습니다. 왼쪽 곱셈 결과에 오른쪽 값을 차례대로 곱셈 class Solution: def productExceptSelf(self, nums: List[int]) -> List[int]: out = [] prod = 1 for idx in range(0, len(nums)): out.append(prod) prod *= nums[idx] prod = 1 for idx in range(len(nums)-1, -1, -1): out[idx] = out[idx]*prod prod = prod*nums[idx] return out 이 문제에서 배워야할 포인트 나눗셈을 하지 않고 O(n)에 풀이해야 한다 풀이 방법은 한 가지 뿐이다. 자기 자신을 제외하고 왼쪽의 곱셈 결과와 오른쪽의 곱셈 결과.. 더보기
[개체지향 프로그래밍] static | 싱글턴 | 내포 클래스 static, 싱글턴, 내포 클래스 static 모든 것이 개체 속에 있는 불편함 이런 단순한 계산도 개체를 만들어서 해야 하나? 개체 단위기 아니라 클래스 단위에서 뭔가를 하고 싶을 때는? 정적 멤버 함수 예 // Math.java public class Math{ public static int abs(int n){ return n b ? a : b; } } // 메인 함수 int absValue = Math.abs(-2); int minValue = Math.min(100, -2.. 더보기
[Python] Leet Code 561. Array Partition I 본 내용은 를 참고했습니다. 파이썬다운 방식 class Solution: def arrayPairSum(self, nums: List[int]) -> int: return sum(sorted(nums)[::2]) 이 문제에서 배워야할 포인트 파이썬다운 방식으로 풀면 한 줄로 해결이 가능하다. 풀이 방식은 리스트를 정렬한 후, 두 개씩 묶어 작은 값의 합을 구하는 것이다. 결국 묶을 필요 없이, 홀수 번째의 값을 두 칸씩 건너 뛰어 더해주면 된다. 더보기