[LeetCode 232, 283, 290 - 완료, 485, 492] python
그냥 하나 풀 때마다 하나씩 여기에 정리해야겠다.
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
"""