Skip to content

Commit daec629

Browse files
committed
6 more
1 parent 09c57bc commit daec629

File tree

10 files changed

+210
-0
lines changed

10 files changed

+210
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Given an input string, reverse the string word by word. A word is defined as a sequence of non-space characters.
2+
3+
The input string does not contain leading or trailing spaces and the words are always separated by a single space.
4+
5+
For example,
6+
Given s = "the sky is blue",
7+
return "blue is sky the".
8+
9+
Could you do it in-place without allocating extra space?
10+
11+
Related problem: Rotate Array
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution(object):
2+
def reverseWords(self, s):
3+
"""
4+
:type s: a list of 1 length strings (List[str])
5+
:rtype: nothing
6+
"""
7+
if not s:
8+
return
9+
start=-1
10+
for i, c in enumerate(s):
11+
if c==" ":
12+
if start>=0:
13+
for k in range((i-start)/2):
14+
s[start+k], s[i-1-k] = s[i-1-k], s[start+k]
15+
start = -1
16+
else:
17+
if start<0:
18+
start = i
19+
i = len(s)
20+
if start>=0:
21+
for k in range((i-start)/2):
22+
s[start+k], s[i-1-k] = s[i-1-k], s[start+k]
23+
s.reverse()
24+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list.
2+
3+
For example,
4+
Assume that words = ["practice", "makes", "perfect", "coding", "makes"].
5+
6+
Given word1 = ��coding��, word2 = ��practice��, return 3.
7+
Given word1 = "makes", word2 = "coding", return 1.
8+
9+
Note:
10+
You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution(object):
2+
def shortestDistance(self, words, word1, word2):
3+
"""
4+
:type words: List[str]
5+
:type word1: str
6+
:type word2: str
7+
:rtype: int
8+
"""
9+
idx1 = -1
10+
idx2 = -1
11+
shortest = len(words)+1
12+
for i, word in enumerate(words):
13+
if word==word1:
14+
idx1 = i
15+
if idx2>=0 and idx1-idx2<shortest:
16+
shortest = idx1-idx2
17+
elif word==word2:
18+
idx2 = i
19+
if idx1>=0 and idx2-idx1<shortest:
20+
shortest = idx2-idx1
21+
return shortest
22+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
This is a follow up of Shortest Word Distance. The only difference is now you are given the list of words and your method will be called repeatedly many times with different parameters. How would you optimize it?
2+
3+
Design a class which receives a list of words in the constructor, and implements a method that takes two words word1 and word2 and return the shortest distance between these two words in the list.
4+
5+
For example,
6+
Assume that words = ["practice", "makes", "perfect", "coding", "makes"].
7+
8+
Given word1 = ��coding��, word2 = ��practice��, return 3.
9+
Given word1 = "makes", word2 = "coding", return 1.
10+
11+
Note:
12+
You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from collections import defaultdict
2+
class WordDistance(object):
3+
def __init__(self, words):
4+
"""
5+
initialize your data structure here.
6+
:type words: List[str]
7+
"""
8+
self.indice = defaultdict(list)
9+
self.memo = {}
10+
self.MAXLEN = len(words)
11+
for i, word in enumerate(words):
12+
self.indice[word].append(i)
13+
14+
def shortest(self, word1, word2):
15+
"""
16+
Adds a word into the data structure.
17+
:type word1: str
18+
:type word2: str
19+
:rtype: int
20+
"""
21+
if (word1, word2) in self.memo:
22+
return self.memo[(word1, word2)]
23+
24+
l1, l2 = self.indice[word1], self.indice[word2]
25+
26+
idx1, idx2 = 0, 0
27+
min_distance = self.MAXLEN
28+
while True:
29+
if idx1 >= len(l1) or idx2 >= len(l2):
30+
break
31+
if l1[idx1]<l2[idx2]:
32+
if l2[idx2]-l1[idx1]<min_distance:
33+
min_distance = l2[idx2]-l1[idx1]
34+
idx1 +=1
35+
else:
36+
if l1[idx1]-l2[idx2]<min_distance:
37+
min_distance = l1[idx1]-l2[idx2]
38+
idx2+=1
39+
self.memo[(word1, word2)] = min_distance
40+
return min_distance
41+
42+
43+
# Your WordDistance object will be instantiated and called as such:
44+
# wordDistance = WordDistance(words)
45+
# wordDistance.shortest("word1", "word2")
46+
# wordDistance.shortest("anotherWord1", "anotherWord2")
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution(object):
2+
def shortestWordDistance(self, words, word1, word2):
3+
"""
4+
:type words: List[str]
5+
:type word1: str
6+
:type word2: str
7+
:rtype: int
8+
"""
9+
if word1!=word2:
10+
idx1 = -1
11+
idx2 = -1
12+
shortest = len(words)+1
13+
for i, word in enumerate(words):
14+
if word==word1:
15+
idx1 = i
16+
if idx2>=0 and idx1-idx2<shortest:
17+
shortest = idx1-idx2
18+
elif word==word2:
19+
idx2 = i
20+
if idx1>=0 and idx2-idx1<shortest:
21+
shortest = idx2-idx1
22+
return shortest
23+
else:
24+
idx = -1
25+
shortest = len(words)
26+
for i, word in enumerate(words):
27+
if word==word1:
28+
if idx>=0:
29+
shortest = min(shortest, i-idx)
30+
idx = i
31+
return shortest
32+

246_strobogrammatic_number/strobo.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution(object):
2+
def isStrobogrammatic(self, num):
3+
"""
4+
:type num: str
5+
:rtype: bool
6+
"""
7+
pairs = {"1":"1","0":"0", "8":"8", "6":"9","9":"6"}
8+
if len(num)%2==0:
9+
half = len(num)/2
10+
else:
11+
half = len(num)/2+1
12+
for i in range(half):
13+
if num[i] not in pairs:
14+
return False
15+
if pairs[num[i]]!=num[len(num)-1-i]:
16+
return False
17+
return True
18+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).
2+
3+
Find all strobogrammatic numbers that are of length = n.
4+
5+
For example,
6+
Given n = 2, return ["11","69","88","96"].
7+
8+
Hint:
9+
10+
Try to use recursion and notice that it should recurse with n - 2 instead of n - 1.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution(object):
2+
pairs = [("0","0"), ("1","1"),("8","8"),("6","9"), ("9","6")]
3+
def findStrobogrammatic(self, n):
4+
"""
5+
:type n: int
6+
:rtype: List[str]
7+
"""
8+
return self.recurse(n, n)
9+
10+
def recurse(self, n, N):
11+
if n==0:
12+
return [""]
13+
elif n==1:
14+
return ["0", "1", "8"]
15+
else:
16+
result = []
17+
if n!=N:
18+
for s in self.recurse(n-2, N):
19+
for prefix, suffix in self.pairs:
20+
result.append(prefix+s+suffix)
21+
else:
22+
for s in self.recurse(n-2, N):
23+
for prefix, suffix in self.pairs[1:]:
24+
result.append(prefix+s+suffix)
25+
return result

0 commit comments

Comments
 (0)