Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions MovingAveragefromDataStream.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class MovingAverage(object):

def __init__(self, size):
"""
Initialize your data structure here.
:type size: int
"""
self.Size = size
self.data = [None]*size
self.head = -1
self.tail = -1

def next(self, val):
"""
:type val: int
:rtype: float
"""
if self.head == -1:
self.head = 0
self.tail = 0
self.data[0] = val
return float(val)
elif (self.tail+1) % self.Size != self.head:
self.tail += 1
self.data[self.tail] = val
return float(sum(self.data[:self.tail+1]))/(self.tail-self.head+1)
else:
self.head = (self.head + 1) % self.Size
self.tail = (self.tail + 1) % self.Size
self.data[self.tail] = val
return float(sum(self.data))/self.Size



# Your MovingAverage object will be instantiated and called as such:
# obj = MovingAverage(size)
# param_1 = obj.next(val)
109 changes: 109 additions & 0 deletions DesignCircularQueue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
class MyCircularQueue(object):

def __init__(self, k):
"""
Initialize your data structure here. Set the size of the queue to be k.
:type k: int
"""
self.size = k
self.data = [None]*k
self.head = -1
self.tail = -1

def enQueue(self, value):
"""
Insert an element into the circular queue. Return true if the operation is successful.
:type value: int
:rtype: bool
"""
if self.isEmpty():
self.head += 1
self.tail += 1
self.data[0] = value
return True
elif not self.isFull():
if self.tail != self.size - 1:
self.tail += 1
self.data[self.tail] = value
return True
else:
self.tail = 0
self.data[self.tail] = value
return True
else:
return False


def deQueue(self):
"""
Delete an element from the circular queue. Return true if the operation is successful.
:rtype: bool
"""
if self.isEmpty():
return False
elif self.head == self.tail:
self.head, self.tail = -1, -1
return True
else:
if self.head != self.size - 1:
self.head += 1
return True
else:
self.head = 0
return True



def Front(self):
"""
Get the front item from the queue.
:rtype: int
"""
if self.isEmpty():
return -1
else:
return self.data[self.head]



def Rear(self):
"""
Get the last item from the queue.
:rtype: int
"""
if self.isEmpty():
return -1
else:
return self.data[self.tail]



def isEmpty(self):
"""
Checks whether the circular queue is empty or not.
:rtype: bool
"""
if self.head == -1 and self.tail == -1:
return True
else:
return False


def isFull(self):
"""
Checks whether the circular queue is full or not.
:rtype: bool
"""
if self.head == ( self.tail + 1 ) % self.size:
return True
else:
return False

# Your MyCircularQueue object will be instantiated and called as such:
# obj = MyCircularQueue(k)
# param_1 = obj.enQueue(value)
# param_2 = obj.deQueue()
# param_3 = obj.Front()
# param_4 = obj.Rear()
# param_5 = obj.isEmpty()
# param_6 = obj.isFull()
17 changes: 17 additions & 0 deletions leetCode_10.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'''
Write a function that takes a string as input and returns the string reversed.

Example:
Given s = "hello", return "olleh".
'''

class Solution:
def reverseString(self, s):
"""
:type s: str
:rtype: str
"""
ans = ''
for x in range(len(s)):
ans = ans + s[len(s)-x-1]
return ans
28 changes: 28 additions & 0 deletions leetCode_11.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'''
Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), ..., (an, bn) which makes sum of min(ai, bi) for all i from 1 to n as large as possible.

Example 1:
Input: [1,4,3,2]

Output: 4
Explanation: n is 2, and the maximum sum of pairs is 4 = min(1, 2) + min(3, 4).

'''

class Solution:
def arrayPairSum(self, nums):
"""
:type nums: List[int]
:rtype: int
"""

n=sorted(nums)
sum = 0
for x in range(0,len(n),2):
sum = sum + n[x]
return sum


'''
return (sum(sorted(nums)[0::2]))
'''
37 changes: 37 additions & 0 deletions leetCode_12.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'''
Given an array nums and a value val, remove all instances of that value in-place and return the new length.

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

The order of elements can be changed. It doesn't matter what you leave beyond the new length.

Given nums = [0,1,2,2,3,0,4,2], val = 2,

Your function should return length = 5, with the first five elements of nums containing 0, 1, 3, 0, and 4.

Note that the order of those five elements can be arbitrary.

It doesn't matter what values are set beyond the returned length.
'''

class Solution:
def removeElement(self, nums, val):
"""
:type nums: List[int]
:type val: int
:rtype: int
"""




i,t,j = 0,len(nums),0

while j < t:
if nums[i] == val:
nums.remove(val)
i -= 1
i += 1
j += 1

return len(nums)
31 changes: 31 additions & 0 deletions leetCode_13.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'''
Given a binary array, find the maximum number of consecutive 1s in this array.

Example 1:
Input: [1,1,0,1,1,1]
Output: 3
Explanation: The first two digits or the last three digits are consecutive 1s.
The maximum number of consecutive 1s is 3.
Note:

The input array will only contain 0 and 1.
The length of input array is a positive integer and will not exceed 10,000
'''
class Solution:
def findMaxConsecutiveOnes(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
rec,curRec = 0,0

for i in nums:
if i==1:
curRec += 1
else:
if curRec>rec:
rec=curRec
curRec = 0
if curRec>rec:
rec=curRec
return rec
41 changes: 41 additions & 0 deletions leetCode_14.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'''
Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. If there isn't one, return 0 instead.

Example:

Input: s = 7, nums = [2,3,1,2,4,3]
Output: 2
Explanation: the subarray [4,3] has the minimal length under the problem constraint.
Follow up:
If you have figured out the O(n) solution, try coding another solution of which the time complexity is O(n log n).
'''
# 我不太懂最後這一行,為什麼既然已經用了O(n)的方法,還要找O(nlog n)的方法?


class Solution:
def minSubArrayLen(self, s, nums):
"""
:type s: int
:type nums: List[int]
:rtype: int
"""

i,j,curSum=0,0,0
curLen,ansLen=0,len(nums)
success = False

for x in nums:
curSum = curSum + x
while curSum>=s:
success = True
curLen = j - i + 1
if curLen =< ansLen:
ansLen = curLen
curSum = curSum - nums[i]
i += 1
j += 1

if success:
return ansLen
else:
return 0
47 changes: 47 additions & 0 deletions leetCode_15.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'''
Given an array, rotate the array to the right by k steps, where k is non-negative.

Example 1:

Input: [1,2,3,4,5,6,7] and k = 3
Output: [5,6,7,1,2,3,4]
Explanation:
rotate 1 steps to the right: [7,1,2,3,4,5,6]
rotate 2 steps to the right: [6,7,1,2,3,4,5]
rotate 3 steps to the right: [5,6,7,1,2,3,4]
Example 2:

Input: [-1,-100,3,99] and k = 2
Output: [3,99,-1,-100]
Explanation:
rotate 1 steps to the right: [99,-1,-100,3]
rotate 2 steps to the right: [3,99,-1,-100]
Note:

Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
Could you do it in-place with O(1) extra space?

'''
class Solution:
def rotate(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: void Do not return anything, modify nums in-place instead.
"""
n = len(nums)
k = k % n
nums[:] = nums[n-k:] + nums[:n-k]

# 以下是在討論區挖到的,可我還是不懂為什麼寫成第二種,nums就會被指向新的nums而第一種不會?
'''
A little important thing to be cautious:

nums[:] = nums[n-k:] + nums[:n-k]
can't be written as:

nums = nums[n-k:] + nums[:n-k]
on the OJ.

The previous one can truly change the value of old nums, but the following one just changes its reference to a new nums not the value of old nums.
'''
Loading