|
| 1 | +## Inverview5:字符串替换空格 |
| 2 | + |
| 3 | +**题目**:请实现一个函数,把字符串中的每个空格替换成“%20”。例如,输入“We are happy.”,则输出“We%20are%20happy.”。 |
| 4 | + |
| 5 | +1. 首先遍历输入字符串有多少个空格。 |
| 6 | +2. 根据空格数可以计算新字符串的长度,并且创建该长度的数组 |
| 7 | +3. 再次遍历字符串,如果碰到空格,就把空格前面的字符串赋给新数组,并添加上需要替换空格的字符。 |
| 8 | +4. 把新创建的数组变换成字符串。 |
| 9 | + |
| 10 | +**时间复杂度:O(n)** |
| 11 | + |
| 12 | +```python |
| 13 | + |
| 14 | +## start |
| 15 | +def replace_space_str(input_str, replace_str): |
| 16 | + |
| 17 | + if input_str == '' or input_str == None or replace_str == None: |
| 18 | + print('输入字符串不合法!') |
| 19 | + return |
| 20 | + |
| 21 | + ##遍历输入字符串找出空格数 |
| 22 | + space_num = 0 |
| 23 | + for char in input_str: |
| 24 | + if char == ' ': |
| 25 | + space_num += 1 |
| 26 | + |
| 27 | + ## 创建新字符串数组 |
| 28 | + new_len = len(input_str) + (len(replace_str) - 1) * space_num |
| 29 | + new_str = new_len * [None] |
| 30 | + |
| 31 | + index = 0 |
| 32 | + index_new_str = 0 |
| 33 | + for i in range(len(input_str)): |
| 34 | + if input_str[i] == ' ': |
| 35 | + ri_index_new_str = index_new_str + i - index |
| 36 | + |
| 37 | + new_str[index_new_str : ri_index_new_str] = list(input_str[index : i]) |
| 38 | + new_str[ri_index_new_str : ri_index_new_str + len(replace_str)] = list(replace_str) |
| 39 | + index_new_str = ri_index_new_str + len(replace_str) |
| 40 | + index = i + 1 |
| 41 | + |
| 42 | + if i == len(input_str) - 1: |
| 43 | + new_str[index_new_str : len(new_str)] = list(input_str[index : i + 1]) |
| 44 | + |
| 45 | + |
| 46 | + print(''.join(new_str)) |
| 47 | + |
| 48 | +input_str = 'We are happy.' |
| 49 | +replace_str = '%20' |
| 50 | +replace_space_str(input_str, replace_str) |
| 51 | +``` |
| 52 | + |
| 53 | +We%20are%20happy. |
| 54 | + |
| 55 | +**Inverview5测试用例** |
| 56 | + |
| 57 | +```python |
| 58 | +## 输入的字符串中包含空格(空格位于字符串的最前面;空格位于字符串的最后面;空格位于字符串的中间;字符串中有连续多个空格)。 |
| 59 | +input_str = ' We are happy. ' |
| 60 | +replace_str = '%20' |
| 61 | +replace_space_str(input_str, replace_str) |
| 62 | + |
| 63 | +## 输入的字符串中没有空格。 |
| 64 | +input_str = 'Wearehappy.' |
| 65 | +replace_str = '%20' |
| 66 | +replace_space_str(input_str, replace_str) |
| 67 | + |
| 68 | +## 字符串是None |
| 69 | +input_str = None |
| 70 | +replace_str = '%20' |
| 71 | +replace_space_str(input_str, replace_str) |
| 72 | + |
| 73 | +## 字符串是空串 |
| 74 | +input_str = '' |
| 75 | +replace_str = '%20' |
| 76 | +replace_space_str(input_str, replace_str) |
| 77 | + |
| 78 | +## 字符串只有一个空格字符 |
| 79 | +input_str = ' ' |
| 80 | +replace_str = '%20' |
| 81 | +replace_space_str(input_str, replace_str) |
| 82 | +``` |
| 83 | + |
| 84 | +%20We%20are%20%20happy.%20 |
| 85 | + |
| 86 | +Wearehappy. |
| 87 | + |
| 88 | +输入字符串不合法! |
| 89 | + |
| 90 | +输入字符串不合法! |
| 91 | + |
| 92 | +%20 |
| 93 | + |
| 94 | +------ |
| 95 | + |
| 96 | +> 作者:[@mantchs](https://github.com/NLP-LOVE/ML-NLP) |
| 97 | +> |
| 98 | +> GitHub:[https://github.com/NLP-LOVE/CodingInterviews2-ByPython](https://github.com/NLP-LOVE/CodingInterviews2-ByPython) |
| 99 | +> |
| 100 | +> 有意向一起完成此项目或者有问题、有补充的可以加入《剑指offer》讨论群【818185620】<a target="_blank" href="//shang.qq.com/wpa/qunwpa?idkey=8c188e86e0eac4a214861c2c706a9c0baf75176e16e52f07b8a64d1a13f99a0d"><img border="0" src="http://pub.idqqimg.com/wpa/images/group.png" alt="《剑指offer》讨论群" title="《剑指offer》讨论群"></a> |
0 commit comments