Problem: 271. Code and decoding of string
[TOC]
Thinking
Is the first reaction encrypted and decrypted the string,But after reading the question, I found that the list was converted into a string,Then the second part of the string that he has processed back to the source list。
Solution
There may be multiple elements in the list(Depend on‘,’The word group that is separated),在每个元素middle可能存在多个Depend on空格隔开的单词。So separatelistThe elements and spaces are processed。
- First of all, the first layer traverses on the list,Each element traverses every element,Read‘ ’When the pre -declared empty list is added, adding a rotary symbol(In the beginning, I plan to customize the righteous symbol,Later discovered\tDirectly feasible)Add a new one to the list at each layer to end each layer,and‘ ’Different rotation are such as\n。at last
''.join()
Convert to a string,return。 - When decoding,Pre -declaration of a empty listListAnd empty stringStr,Read when traversing‘\t’when,Add the space to the string
Str = ''.join([Str, ' '])
,Same,Read’\n’when就将Stradd toListmiddle,WillStrPlace the next cycle。
the complexity
时间the complexity:
添加时间the complexity, Exemplary example: $O(n)$
空间the complexity:
添加空间the complexity, Exemplary example: $O(n)$
Code
1 |
|