본문 바로가기

Python81

LeetCode 283. Move Zeroes - Python LeetCode 283. Move Zeroes - Python Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements. Example: Input: [0,1,0,3,12] Output: [1,3,12,0,0] Solution : #[::-1]는 처음부터 끝까지 -1칸 간격으로 ( == 역순으로) #pop()는 리스트에서 주어진 위치에 있는 항목을 삭제하고, 그 항목을 돌려줌 class Solution: def moveZeroes(self, nums): for i in range(len(nums))[::-1]: if nums[.. 2020. 8. 16.
LeetCode 169. Majority Element - Python LeetCode 169. Majority Element - Python Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. You may assume that the array is non-empty and the majority element always exist in the array. Example 1: Input: [3,2,3] Output: 3 Example 2: Input: [2,2,1,1,1,2,2] Output: 2 Solution 1: #Boyer–Moore majority vote algorithm 사용 clas.. 2020. 8. 13.
LeetCode 206. Reverse Linked List - Python LeetCode 206. Reverse Linked List - Python Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5->4->3->2->1->NULL Solution: class Solution(object): def reverseList(self, head): prev = None curr = head while curr!= None: tmp = curr.next curr.next = prev prev = curr curr = tmp return prev (참고 사이트) 링크드리스트 역순 재배열 : https://www.youtube.com/watch?v=gf_BiXt4YlQ 2020. 8. 11.
[Python 기초] 연결리스트 (Linked List) #NODE class Node: #define function called 'Node' def __init__(self, item) : self.val = item #save item in 'val' and this node is the head of linked list self.next = None #'next' means the other node #ADD def add(self, item): cur = self.head while cur.next is not None: #cur.next is not None so goes next cur = cur.next cur.next = Node(item) #since the next is none, generate new node #PRINT def pri.. 2020. 8. 10.
LeetCode 226. Invert Binary Tree - Python LeetCode 226. Invert Binary Tree - Python Invert a binary tree. Example: Input: 4 / \ 2 7 / \ / \ 1 3 6 9 Output: 4 / \ 7 2 / \ / \ 9 6 3 1 Solution : class Solution(object): def invertTree(self, root): stack = [root] while stack: node = stack.pop() if node: node.left, node.right = node.right, node.left stack += node.left, node.right return root (솔루션 출처) https://gist.github.com/ryuji0123/ 더보기 # .. 2020. 8. 9.
LeetCode 136. Single Number - Python LeetCode 136. Single Number - Python Given a non-empty array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? Example 1: Input: [2,2,1] Output: 1 Example 2: Input: [4,1,2,1,2] Output: 4 Solution 1 : class Solution: def singleNumber(self, nums: List[int]) .. 2020. 8. 6.
LeetCode 104. Maximum Depth of Binary Tree - Python LeetCode 104. Maximum Depth of Binary Tree - Python Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. Note: A leaf is a node with no children. Example: Given binary tree [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 return its depth = 3. Solution 1 (Recursive) : class Solution: def maxDep.. 2020. 8. 4.
LeetCode 617. Merge Two Binary Trees - Python LeetCode 617. Merge Two Binary Trees - Python Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not. You need to merge them into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as .. 2020. 8. 3.
[코드업 기초 100제] 1001~1099 (파이썬) - 문제 풀이용 CodeUp에서 제공한 문제를 Python3으로 풀었습니다. '더보기'를 클릭하면 정답 코드가 보입니다. #1001 : [기초-출력] 출력하기01 다음 단어를 출력하시오. Hello 더보기 print("Hello") #1002 : [기초-출력] 출력하기02 다음 문장을 출력해보자. Hello World (대소문자에 주의한다.) 더보기 print("Hello World") #1003 : [기초-출력] 출력하기03 다음과 같이 줄을 바꿔 출력해야 한다. Hello World (두 줄에 걸쳐 줄을 바꿔 출력) 더보기 print("Hello\nWorld") #1004 : [기초-출력] 출력하기04 다음 문장을 출력하시오. 'Hello' 더보기 print("'Hello'") #1005 : [기초-출력] 출력하기0.. 2020. 7. 28.
[코드업 기초 100제] 1001~1099 (파이썬) #1001 : [기초-출력] 출력하기01 print("Hello") #1002 : [기초-출력] 출력하기02 print("Hello World") #1003 : [기초-출력] 출력하기03 print("Hello\nWorld") #1004 : [기초-출력] 출력하기04 print("'Hello'") #1005 : [기초-출력] 출력하기05 print('"Hello World"') #1006 : [기초-출력] 출력하기06 print('"!@#$%^&*()"') #1007 : [기초-출력] 출력하기07 print('"C:\Download\hello.cpp"') #1008 : [기초-출력] 출력하기08 print('''\ #따옴표 3개는 줄바꿈 그대로 출력 ┌┬┐ ├┼┤ └┴┘ ''') (9번 문제는 없음) #.. 2020. 7. 28.