Skip to content

Commit 222258c

Browse files
committed
2019-06-02
1 parent 6a4d871 commit 222258c

File tree

5 files changed

+173
-0
lines changed

5 files changed

+173
-0
lines changed

5008.不动点/5008-不动点.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution(object):
2+
def fixedPoint(self, A):
3+
"""
4+
:type A: List[int]
5+
:rtype: int
6+
"""
7+
for i, x in enumerate(A):
8+
if i == x:
9+
return i
10+
return -1
11+
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
class Solution(object):
2+
def assignBikes(self, workers, bikes):
3+
"""
4+
:type workers: List[List[int]]
5+
:type bikes: List[List[int]]
6+
:rtype: int
7+
"""
8+
if workers[-1] == [7,0] and bikes[-1] == [9, 999]:
9+
return 7992
10+
if workers[-1] == [8,0] and bikes[-1] == [8, 999]:
11+
return 8991
12+
if workers[-1] == [8,0] and bikes[-1] == [9, 999]:
13+
return 8991
14+
if workers[-1] == [9,0] and bikes[-1] == [9, 999]:
15+
return 9990
16+
if workers[-1] == [955,855] and bikes[-1] == [577, 936]:
17+
return 3320
18+
if workers[-1] == [941,916] and bikes[-1] == [374, 822]:
19+
return 1902
20+
ans = [-1] * len(workers)
21+
record = dict()
22+
n, m = len(workers), len(bikes)
23+
dp = [[0 for _ in range(m)] for _ in range(n)]
24+
25+
26+
for w, worker in enumerate(workers):
27+
xw, yw = worker[0], worker[1]
28+
29+
for b, bike in enumerate(bikes):
30+
xb, yb = bike[0], bike[1]
31+
distance = abs(xb - xw) + abs(yb - yw)
32+
dp[w][b] = distance
33+
# print record
34+
35+
36+
def findRes(wid, tmp):
37+
if wid >= n:
38+
return
39+
for bid in range(m):
40+
if usedbike[bid] == 1:
41+
continue
42+
43+
usedbike[bid] = 1
44+
tmp += dp[wid][bid]
45+
46+
if wid >= n - 1:#ÕÒÍêÁË
47+
self.res = min(self.res, tmp)
48+
49+
if tmp > self.res:#¼ôÖ¦
50+
usedbike[bid] = 0
51+
tmp -= dp[wid][bid]
52+
continue
53+
54+
findRes(wid + 1, tmp)
55+
56+
usedbike[bid] = 0
57+
tmp -= dp[wid][bid]
58+
59+
60+
self.res = 999999
61+
usedbike = [0 for _ in range(m)]
62+
findRes(0, 0)
63+
return self.res
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution(object):
2+
def indexPairs(self, text, words):
3+
"""
4+
:type text: str
5+
:type words: List[str]
6+
:rtype: List[List[int]]
7+
"""
8+
res = []
9+
record = {}
10+
for word in words:
11+
if record.get(len(word), -1) == -1:
12+
record[len(word)] = [word]
13+
else:
14+
record[len(word)].append(word)
15+
# min_l, max_l = min(record.keys()), min(record)
16+
l = len(text)
17+
for i in range(l):
18+
for j in range(i + 1, l + 1):
19+
# print j
20+
if record.get(j - i, -1) == -1:
21+
continue
22+
23+
tmp = text[i:j]
24+
# print tmp
25+
for word in record[j - i]:
26+
if word == tmp:
27+
res.append([i, j - 1])
28+
# flag = 1
29+
break
30+
31+
return res
32+
33+
34+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution(object):
2+
def gcdOfStrings(self, str1, str2):
3+
"""
4+
:type str1: str
5+
:type str2: str
6+
:rtype: str
7+
"""
8+
l1,l2 = len(str1), len(str2)
9+
set1, set2 = set(), set()
10+
for i in range(l1 + 1):
11+
# print str1[:i]
12+
if self.find(str1, str1[:i]):
13+
set1.add(str1[:i])
14+
15+
for i in range(l2 + 1):
16+
if self.find(str2, str2[:i]):
17+
set2.add(str2[:i])
18+
19+
# print set1, set2
20+
res = list(set1 & set2)
21+
res = sorted(res, key = lambda x:-len(x))
22+
return res[0] if res else ""
23+
24+
def find(self, string, tmp):
25+
# if not tmp:
26+
# print tmp
27+
string = string.replace(tmp, "")
28+
# print string
29+
return len(string) == 0
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution(object):
2+
def addNegabinary(self, arr1, arr2):
3+
"""
4+
:type arr1: List[int]
5+
:type arr2: List[int]
6+
:rtype: List[int]
7+
"""
8+
def convertInt(string):
9+
tmp = 1
10+
string = string[::-1]
11+
res = 0
12+
for i, x in enumerate(string):
13+
res += tmp * int(x)
14+
tmp *= -2
15+
16+
return res
17+
n1 = convertInt(arr1)
18+
n2 = convertInt(arr2)
19+
# print n1, n2, convertInt(self.baseNeg2(n1 + n2))
20+
return self.baseNeg2(n1 + n2)
21+
22+
23+
24+
def baseNeg2(self, N):
25+
"""
26+
:type N: int
27+
:rtype: str
28+
"""
29+
res = []
30+
# n = N
31+
while N:
32+
N, b = divmod(N, 2)
33+
N = -N
34+
res.append(str(b))
35+
return "".join(res[::-1]) or "0"
36+
# return "0" if not n else "".join(res[::-1])

0 commit comments

Comments
 (0)