Skip to content

Commit 0ea2d7b

Browse files
committedMar 27, 2020
2020-03-27
1 parent 547f53a commit 0ea2d7b

File tree

4 files changed

+83
-0
lines changed

4 files changed

+83
-0
lines changed
 
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution(object):
2+
def createTargetArray(self, nums, index):
3+
"""
4+
:type nums: List[int]
5+
:type index: List[int]
6+
:rtype: List[int]
7+
"""
8+
res = []
9+
for i in range(len(nums)):
10+
res.insert(index[i], nums[i])
11+
return res

‎1390.四因数/1390-四因数.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution(object):
2+
def sumFourDivisors(self, nums):
3+
"""
4+
:type nums: List[int]
5+
:rtype: int
6+
"""
7+
def factor(n):
8+
l = []
9+
for i in range(1, int(n ** 0.5) + 1):
10+
if n % i == 0:
11+
l.append(i)
12+
if n / i != i:
13+
l.append(n / i)
14+
if len(l) > 4:
15+
break
16+
17+
return sum(l) if len(l) == 4 else 0
18+
19+
return sum(map(factor, nums))
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution(object):
2+
def hasValidPath(self, grid):
3+
"""
4+
:type grid: List[List[int]]
5+
:rtype: bool
6+
"""
7+
m, n = len(grid), len(grid[0])
8+
dir = {}
9+
dir[1] = [0, 1, 0, 1]
10+
dir[2] = [1, 0, 1, 0]
11+
dir[3] = [0, 0, 1, 1]
12+
dir[4] = [0, 1, 1, 0]
13+
dir[5] = [1, 0, 0, 1]
14+
dir[6] = [1, 1, 0, 0]
15+
16+
dx = [-1, 0, 1, 0]
17+
dy = [0, 1, 0, -1]
18+
queue = [(0, 0)]
19+
visited = set(queue)
20+
21+
cor = {0:2, 1:3, 3:1, 2:0}
22+
while queue:
23+
x0, y0 = queue.pop()
24+
if [x0, y0] == [m - 1, n - 1]:
25+
return True
26+
for k in range(4):
27+
x = x0 + dx[k]
28+
y = y0 + dy[k]
29+
if 0 <= x < m and 0 <= y < n and (x, y) not in visited and dir[grid[x0][y0]][k] & dir[grid[x][y]][cor[k]]:
30+
visited.add((x, y))
31+
queue.append((x, y))
32+
return False
33+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution(object):
2+
def longestPrefix(self, s):
3+
"""
4+
:type s: str
5+
:rtype: str
6+
"""
7+
base = 131
8+
mod = 10 ** 9 + 7
9+
res = ""
10+
prefix, suffix = 0, 0
11+
multiple = 1
12+
for i in range(len(s) - 1):
13+
prefix = (prefix * base + ord(s[i])) % mod
14+
suffix = (ord(s[-(i + 1)]) * multiple + suffix) % mod
15+
if prefix == suffix:
16+
res = s[:i + 1]
17+
18+
multiple = multiple * base % mod
19+
20+
return res

0 commit comments

Comments
 (0)
Please sign in to comment.