Skip to content

Commit 1476880

Browse files
committed
2019-06-29
1 parent 74ae25b commit 1476880

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution(object):
2+
def twoSumLessThanK(self, A, K):
3+
"""
4+
:type A: List[int]
5+
:type K: int
6+
:rtype: int
7+
"""
8+
if len(A) < 2:
9+
return -1
10+
tmp = []
11+
for i in range(len(A)):
12+
for j in range(i + 1, len(A)):
13+
tmp.append(A[i] + A[j])
14+
15+
tmp.sort()
16+
if tmp[0] > K:
17+
return -1
18+
# print tmp
19+
res = tmp[0]
20+
for item in tmp:
21+
if item < K and abs(item - K) < abs(res - K):
22+
res = item
23+
return res
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution(object):
2+
def numKLenSubstrNoRepeats(self, S, K):
3+
"""
4+
:type S: str
5+
:type K: int
6+
:rtype: int
7+
"""
8+
if len(S) < K:
9+
return 0
10+
res = 0
11+
for i in range(len(S) - K + 1):
12+
# for j in range(i + K, len(S)):
13+
tmp = S[i:i + K]
14+
if self.check(tmp):
15+
res += 1
16+
return res
17+
18+
19+
def check(self, string):
20+
# record = {}
21+
from collections import Counter
22+
record = Counter(string)
23+
# print record
24+
for key, val in record.items():
25+
if val > 1:
26+
return False
27+
# print string
28+
return True

0 commit comments

Comments
 (0)