본문 바로가기

Python81

[Python 기초] while 반복문 #while 반복문 - 조건이 참인 동안 계속 반복 c = 5 while c!=0: #c가 0이될 때까지 반복 print(c) c-=1 #c가 5에서 -1 반복 #5 #4 #3 #2 #1 #무한루프 시 CTRL + C 로 빠져나올 수 있음 c = 5 while c: print(c) c-=1 #brak문 while True: response = input() if int(response) % 5 == 0 : #10의 배수일경우 print('5으로 나누었을때 나머지가 0입니다.') break #5으로 나누었을때 나머지가 0인 경우 break문으로 반복문을 중단 #Input: 5 #Out: 5으로 나누었을때 나머지가 0입니다. #continue문 - continue문은 반복문을 중단시키지 않고 다음 반복으로 .. 2020. 9. 6.
[Python 기초] 재귀 알고리즘 (recursive algorithms) #종결조건의 중요성 알아보기 #재귀의 간단한 예시 - 1부터 n까지 sum def sum(n): print(n) if n 2020. 9. 6.
[Python 기초] 탐색 (search) #선형탐색 (Linear Search) def linear_search(L,x): i=0 #index i를 0으로 준다 while i < len(L) and L[i] != x: #i가 L리스트의 길이보다 작고, L의 i번째 원소가 x와 같지 않을때 i += 1 #i를 1씩 증가시켜나간다. #원소가 발견되면 i값을 가지고 loop가 종료하게 될 것이기 때문에 #i가 L리스트의 길이보다 짧으면 리스트 안에서 원소를 발견했다는 의미 if i < len(L): return i #발견된 x를 리턴하고 else: return -1 #그렇지 않으면 -1을 리턴한다. S = [3,8,2,7,6,7,6,10,9] linear_search(S,6) #Out: 4 S = [3,8,2,7,6,7,6,10,9] linear_s.. 2020. 9. 6.
[Python 기초] 정렬 (sort) #알파벳, 숫자 크기 순으로 정렬 L=['abcd','xyz','spam'] sorted(L) #Out: ['abcd', 'spam', 'xyz'] L=[3,7,2,7,1,7] L.sort() L #Out: [1, 2, 3, 7, 7, 7] #lambda : 익명함수 (lambda x,y: x + y)(5, 6) #Out: 11 ▼ 익명 함수 lambda 활용법 더보기 더보기 #map() list(map(lambda x: x ** 2, range(5))) # 파이썬 2 및 파이썬 3 #Out: [0, 1, 4, 9, 16] #reduce() from functools import reduce reduce(lambda x, y: x + y, [0, 1, 2, 3, 4]) #Out: 10 reduce(lam.. 2020. 9. 5.
LeetCode 101. Symmetric Tree - Python LeetCode 101. Symmetric Tree - Python Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree [1,2,2,3,4,4,3] is symmetric: 1 / \ 2 2 / \ / \ 3 4 4 3 But the following [1,2,2,null,3,null,3] is not: 1 / \ 2 2 \ \ 3 3 Solution : Recursive class Solution: def isSymmetric(self, root: TreeNode) -> bool: if not root: #루트가 없으면 True 반환.. 2020. 9. 4.
LeetCode 1. Two Sum - Python LeetCode 1. Two Sum - Python Given an array of integers nums and and integer target, return the indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order. Example 1: Input: nums = [2,7,11,15], target = 9 Output: [0,1] Output: Because nums[0] + nums[.. 2020. 8. 31.
LeetCode 543. Diameter of Binary Tree - Python LeetCode 543. Diameter of Binary Tree - Python Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root. 이진트리의 지름 구하기 즉 가장 긴 path의 '길이'를 구하면 된다. 이 때, 노드 사이의 길이란 노드 사이의 edge의 개수이다. 예를 들어 아래의 경우 path [4,2,1,3] 나 [5,2,1,3]가 가장 길기 때문에.. 2020. 8. 25.
LeetCode 121. Best Time to Buy and Sell Stock - Python Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit. Note that you cannot sell a stock before you buy one. Example 1: Input: [7,1,5,3,6,4] Output: 5 Explanation: Buy on day 2 (price = 1) and sell on day .. 2020. 8. 24.
LeetCode 21. Merge Two Sorted Lists - Python LeetCode 21. Merge Two Sorted Lists - Python Merge two sorted linked lists and return it as a new sorted list. The new list should be made by splicing together the nodes of the first two lists. Example: Input: 1->2->4, 1->3->4 Output: 1->1->2->3->4->4 Solution : class Solution: def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode: if l1 is None : return l2 if l2 is None : return l1 if.. 2020. 8. 21.
LeetCode 448. Find All Numbers Disappeared in an Array LeetCode 448. Find All Numbers Disappeared in an Array - Python Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements of [1, n] inclusive that do not appear in this array. Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space. Example: Input: [4,.. 2020. 8. 18.