8000 Total Runtime: 434 ms · michaelhuang/LintCode_Python@ae0cf6d · GitHub
[go: up one dir, main page]

Skip to content

Commit ae0cf6d

Browse files
committed
1 parent 3aa4578 commit ae0cf6d

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

Space_Replacement.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution:
2+
# @param {char[]} string: An array of Char
3+
# @param {int} length: The true length of the string
4+
# @return {int} The true length of new string
5+
def replaceBlank(self, string, length):
6+
if string is None or length == 0:
7+
return None
8+
9+
space_count, rep_arr = 0, ['%', '2', '0']
10+
for i in string:
11+
if i == ' ':
12+
space_count += 1
13+
14+
true_len = length + 2 * space_count
15+
j, k = length - 1, true_len - 1
16+
while j >= 0:
17+
if string[j] == ' ':
18+
k -= 3
19+
for i in xrange(3):
20+
string[k + 1 + i] = rep_arr[i]
21+
else:
22+
string[k] = string[j]
23+
k -= 1
24+
j -= 1
25+
26+
return true_len
27+
28+
if __name__ == '__main__':
29+
a = Solution()
30+
a1 = ['A', 'B', 'C', ' ', 'D', ' ', '', '', '', '']
31+
print a.replaceBlank(a1, 6)
32+
print a1

0 commit comments

Comments
 (0)
0