Skip to content

Commit befd8b7

Browse files
committed
2019-5-21
1 parent dcbaa89 commit befd8b7

File tree

48 files changed

+257
-68
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+257
-68
lines changed
File renamed without changes.
File renamed without changes.

0080.删除排序数组中的重复项 II/0080-删除排序数组中的重复项 II.py

-12
This file was deleted.

0217.存在重复元素/0217-存在重复元素.py

+6-8
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@ def containsDuplicate(self, nums):
44
:type nums: List[int]
55
:rtype: bool
66
"""
7-
record = dict()
8-
for num in nums:
9-
if record.get(num, 0):
7+
if len(nums) <= 1:
8+
return False
9+
nums.sort()
10+
for index in range(0,len(nums)-1):
11+
if nums[index] == nums[index +1]:#or nums[index] == nums[index-1]:
1012
return True
11-
record[num] = 1
12-
return False
13-
14-
15-
13+
return False

0219.存在重复元素 II/0219-存在重复元素 II.py

-16
This file was deleted.
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution(object):
2+
def minCost(self, costs):
3+
"""
4+
:type costs: List[List[int]]
5+
:rtype: int
6+
"""
7+
if not costs:
8+
return 0
9+
dp = costs
10+
for i in range(1, len(costs)):
11+
dp[i][0] += min(dp[i - 1][1], dp[i - 1][2])
12+
dp[i][1] += min(dp[i - 1][0], dp[i - 1][2])
13+
dp[i][2] += min(dp[i - 1][0], dp[i - 1][1])
14+
return min(dp[-1])
15+
16+
17+
File renamed without changes.
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution(object):
2+
def generatePossibleNextMoves(self, s):
3+
"""
4+
:type s: str
5+
:rtype: List[str]
6+
"""
7+
res = []
8+
for i in range(len(s) - 1):
9+
if s[i:i+2] == "++":
10+
res.append(s[:i] + "--" + s[i+2:])
11+
return res
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# """
2+
# This is the interface that allows for creating nested lists.
3+
# You should not implement it, or speculate about its implementation
4+
# """
5+
#class NestedInteger(object):
6+
# def __init__(self, value=None):
7+
# """
8+
# If value is not specified, initializes an empty list.
9+
# Otherwise initializes a single integer equal to value.
10+
# """
11+
#
12+
# def isInteger(self):
13+
# """
14+
# @return True if this NestedInteger holds a single integer, rather than a nested list.
15+
# :rtype bool
16+
# """
17+
#
18+
# def add(self, elem):
19+
# """
20+
# Set this NestedInteger to hold a nested list and adds a nested integer elem to it.
21+
# :rtype void
22+
# """
23+
#
24+
# def setInteger(self, value):
25+
# """
26+
# Set this NestedInteger to hold a single integer equal to value.
27+
# :rtype void
28+
# """
29+
#
30+
# def getInteger(self):
31+
# """
32+
# @return the single integer that this NestedInteger holds, if it holds a single integer
33+
# Return None if this NestedInteger holds a nested list
34+
# :rtype int
35+
# """
36+
#
37+
# def getList(self):
38+
# """
39+
# @return the nested list that this NestedInteger holds, if it holds a nested list
40+
# Return None if this NestedInteger holds a single integer
41+
# :rtype List[NestedInteger]
42+
# """
43+
44+
class Solution(object):
45+
def depthSum(self, nestedList):
46+
"""
47+
:type nestedList: List[NestedInteger]
48+
:rtype: int
49+
"""
50+
def Sum(weight, l):
51+
if l.isInteger():
52+
return weight * l.getInteger()
53+
return sum(Sum(weight + 1, item) for item in l.getList())
54+
55+
return sum(Sum(1, i) for i in nestedList)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from collections import deque
2+
class MovingAverage(object):
3+
4+
def __init__(self, size):
5+
"""
6+
Initialize your data structure here.
7+
:type size: int
8+
"""
9+
self.queue = deque()
10+
self.Max_size = size
11+
12+
def next(self, val):
13+
"""
14+
:type val: int
15+
:rtype: float
16+
"""
17+
self.queue.append(val)
18+
if len(self.queue) > self.Max_size:
19+
self.queue.popleft()
20+
return 1.0 * sum(self.queue) / len(self.queue)
21+
22+
23+
# Your MovingAverage object will be instantiated and called as such:
24+
# obj = MovingAverage(size)
25+
# param_1 = obj.next(val)
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class Solution(object):
2+
def maxKilledEnemies(self, grid):
3+
"""
4+
:type grid: List[List[str]]
5+
:rtype: int
6+
"""
7+
if not grid or not grid[0]:
8+
return 0
9+
m, n = len(grid), len(grid[0])
10+
res = 0
11+
def count(x0, y0):
12+
cnt = 0
13+
for x in range(x0, -1, -1): #ÏòÉÏ
14+
if grid[x][y0] == "E":
15+
cnt += 1
16+
elif grid[x][y0] == "W":
17+
break
18+
19+
for x in range(x0, m): #ÏòÏÂ
20+
if grid[x][y0] == "E":
21+
cnt += 1
22+
elif grid[x][y0] == "W":
23+
break
24+
25+
for y in range(y0, -1, -1): #Ïò×ó
26+
if grid[x0][y] == "E":
27+
cnt += 1
28+
elif grid[x0][y] == "W":
29+
break
30+
31+
for y in range(y0, n): #ÏòÓÒ
32+
if grid[x0][y] == "E":
33+
cnt += 1
34+
elif grid[x0][y] == "W":
35+
break
36+
37+
return cnt
38+
39+
for i in range(m):
40+
for j in range(n):
41+
if grid[i][j] == "0":
42+
res = max(res, count(i, j))
43+
return res
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Definition for singly-linked list.
2+
# class ListNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.next = None
6+
7+
class Solution(object):
8+
def reverseList(self, head):
9+
"""
10+
:type head: ListNode
11+
:rtype: ListNode
12+
"""
13+
if head is None or head.next is None:
14+
return head
15+
tmp = self.reverseList(head.next)
16+
head.next.next = head
17+
head.next = None
18+
return tmp
19+
20+
def plusOne(self, head):
21+
"""
22+
:type head: ListNode
23+
:rtype: ListNode
24+
"""
25+
head = self.reverseList(head)
26+
tmp = head
27+
head.val += 1
28+
while(head.val > 9):
29+
head.val -= 10
30+
if head.next:
31+
head.next.val += 1
32+
head = head.next
33+
else:
34+
head.next = ListNode(1)
35+
break
36+
return self.reverseList(tmp)

0377.组合总和 Ⅳ/0377-组合总和 Ⅳ.py

-16
This file was deleted.
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class PhoneDirectory(object):
2+
3+
def __init__(self, maxNumbers):
4+
"""
5+
Initialize your data structure here
6+
@param maxNumbers - The maximum numbers that can be stored in the phone directory.
7+
:type maxNumbers: int
8+
"""
9+
self.list = [0 for _ in range(maxNumbers)]
10+
11+
12+
def get(self):
13+
"""
14+
Provide a number which is not assigned to anyone.
15+
@return - Return an available number. Return -1 if none is available.
16+
:rtype: int
17+
"""
18+
for i, x in enumerate(self.list):
19+
if x == 0:
20+
self.list[i] = 1
21+
return i
22+
return -1
23+
24+
def check(self, number):
25+
"""
26+
Check if a number is available or not.
27+
:type number: int
28+
:rtype: bool
29+
"""
30+
return self.list[number] == 0
31+
32+
def release(self, number):
33+
"""
34+
Recycle or release a number.
35+
:type number: int
36+
:rtype: None
37+
"""
38+
self.list[number] = 0
39+
40+
41+
# Your PhoneDirectory object will be instantiated and called as such:
42+
# obj = PhoneDirectory(maxNumbers)
43+
# param_1 = obj.get()
44+
# param_2 = obj.check(number)
45+
# obj.release(number)
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution(object):
2+
def calPoints(self, ops):
3+
"""
4+
:type ops: List[str]
5+
:rtype: int
6+
"""
7+
stack = list()
8+
for i, op in enumerate(ops):
9+
# print stack
10+
if op == "+":
11+
stack.append(stack[-1] + stack[-2])
12+
elif op == "D":
13+
stack.append(2 * stack[-1])
14+
elif op == "C":
15+
stack.pop()
16+
else:
17+
stack.append(int(op))
18+
19+
return sum(stack)

0974.和可被 K 整除的子数组/0974-和可被 K 整除的子数组.py

-16
This file was deleted.

0 commit comments

Comments
 (0)