그냥 하나 풀 때마다 하나씩 여기에 정리해야겠다.
True False 첫글자는 꼭 대문자로 해야한다.
5. 492 : Construct the Rectangle
BST
6. 501. Find Mode in Binary Search Tree
Minimum Absolute Difference in BST
543. Diameter of Binary Tree
563. Binary Tree Tilt
linked list
203. Remove Linked List Elements
206. Reverse Linked List
conversion
504. Base 7
string
557. Reverse Words in a String III
189. Rotate Array
599. Minimum Index Sum of Two Lists
matrix
566. Reshape the Matrix
783. Minimum Distance Between BST Nodes
Hash
705. Design HashSet
그 다음에 graph로 넘어가기
완료 소스
1. 232 : implement Queue using Stacks
class MyQueue(object):
def __init__(self):
self.data = []
self.size = 0
"""
Initialize your data structure here.
"""
def push(self, x):
self.data.append(x)
self.size += 1
#print self.data
"""
Push element x to the back of queue.
:type x: int
:rtype: None
"""
def pop(self):
if(self.size > 0):
self.size -= 1
pop_data = self.data[0]
self.data.remove(pop_data)
return pop_data
return 0
"""
Removes the element from in front of queue and returns that element.
:rtype: int
"""
def peek(self):
if(self.size > 0):
#print self.data[0]
return self.data[0]
else:
return 0
"""
Get the front element.
:rtype: int
"""
def empty(self):
if self.size == 0:
return 1
else:
return 0
"""
Returns whether the queue is empty.
:rtype: bool
"""
# Your MyQueue object will be instantiated and called as such:
# obj = MyQueue()
# obj.push(x)
# param_2 = obj.pop()
# param_3 = obj.peek()
# param_4 = obj.empty()
2. 283 : Move Zeroes
리스트에 대해 많이 배울 수 있었다.
class Solution(object):
def moveZeroes(self, nums):
if len(nums) == 0:
return
for i in range(len(nums)):
if(nums[i] == 0):
nums.remove(0)
nums.append(0)
"""
:type nums: List[int]
:rtype: None Do not return anything, modify nums in-place instead.
"""
3. 290 : word pattern
class Solution(object):
def wordPattern(self, pattern, str):
s = str.split(' ')
if len(s) != len(pattern):
return False
for i in range(len(s)):
for j in range(i + 1, len(s)):
if pattern[i] == pattern[j]:
if s[i] != s[j]:
return False
else:
if s[i] == s[j]:
return False
return True
"""
:type pattern: str
:type str: str
:rtype: bool
"""
4. 485 : Max Consecutive Ones
https://leetcode.com/problems/max-consecutive-ones/submissions/
예외 처리에 주의해야겠다.
class Solution(object):
def findMaxConsecutiveOnes(self, nums):
cnt = 0
max = 0
for i in range(len(nums)):
if nums[i] == 1:
cnt += 1
else:
if max < cnt:
max = cnt
cnt = 0
if max < cnt:
max = cnt
return max
"""
:type nums: List[int]
:rtype: int
"""
'Algorithm > LeetCode 문제 풀이' 카테고리의 다른 글
[LeetCode 485] python - 예외 처리에 주의하기.. (0) | 2019.04.02 |
---|---|
[LeetCode 283] python 리스트(큐/스택) 이용하기 (0) | 2019.04.02 |
[LeetCode 542. 01 Matrix] [python] 4. 재귀 함수 (0) | 2019.03.31 |
[LeetCode 232. Implement Queue using Stacks] [python] 3. 스택을 이용하여 큐를 구현하기 (0) | 2019.03.30 |
[LeetCode 200 Number of Islands] [python] 2. 파이썬 문법에 익숙해지고 있다. (0) | 2019.03.30 |