avatar
Siz Long

My name is Siz. I am a computer science graduate student specializing in backend development with Golang and Python, seeking opportunities in innovative tech projects. My personal website is me.longsizhuo.com .Connect with me on LinkedIn: https://www.linkedin.com/in/longsizhuo/.

  • Resume
  • Archives
  • Categories
  • Photos
  • Music



{{ date }}

{{ time }}

avatar
Siz Long

My name is Siz. I am a computer science graduate student specializing in backend development with Golang and Python, seeking opportunities in innovative tech projects. My personal website is me.longsizhuo.com .Connect with me on LinkedIn: https://www.linkedin.com/in/longsizhuo/.

  • 主页
  • Resume
  • Archives
  • Categories
  • Photos
  • Music

271. Code and decoding of string - Python Add the transposition symbol solution method Dual complexityO(n)

  2024-01-01        
字数统计: 357字   |   阅读时长: 2min

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。

  1. 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。
  2. When decoding,Pre -declaration of a empty listListAnd empty stringStr,Read when traversing‘\t’when,Add the space to the stringStr = ''.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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

class Codec:
def encode(self, strs: list[str]) -> str:
"""Encodes a list of strings to a single string.
"""
List = []
for i in strs:
for j in i:
if j == ' ':
List.append('\t')
else:
List.append(j)
List.append('\n')
List = ''.join(List)
return List

def decode(self, s: str) -> list[str]:
"""Decodes a single string to a list of strings.
"""
List = []
Str = ''
for i in s:
if i == '\t':
Str = ''.join([Str, ' '])
else:
if i != '\n':
Str = ''.join([Str, i])
else:
List.append(Str)
Str = ''
return List
  • Python
  • solved,answer

扫一扫,分享到微信

微信分享二维码
2679.In the matrix and the harmony
2997. Make an array or harmony K The minimum number of operations
目录
  1. 1. Thinking
  2. 2. Solution
  3. 3. the complexity
  4. 4. Code

150 篇 | 131.7k
次 | 人
这里自动载入天数这里自动载入时分秒
2022-2025 loong loong | 新南威尔士龙龙号