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

1138. The path on the letter board One question daily

  2024-01-01        
字数统计: 311字   |   阅读时长: 1min

topic:

2023-02-12.png
1138. The path on the letter board One question daily.md

Thought:

Read the wrong questions,According to the method of random letters above the subtitle version。butzSpecial circumstances are not considered,I thought about it later,Just turn the column into a line,Time complexity isOn3
。Look atylbs answer,Use directlyasciiThe code watch simulates a letter board。

From the starting point (0,0)(0, 0)(0,0) Set off,Simulate each step of movement,Style the movement of each step into the answer。Pay attention to the direction of moving“Left、superior、right、Down”Order。

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
32
33
class Solution:
def alphabetBoardPath(self, target: str) -> str:
str1 = ""
board = ["abcde",
"fghij",
"klmno",
"pqrst",
"uvwxy",
"z"]
now_i = 0
now_j = 0
for i in board:
for j,value in enumerate(i):
pass
for char in target:
# See which line is
for i in range(len(board)):
if char in board[i]:
if i - now_i > 0:
str1 += 'D' * (i - now_i)
if i - now_i < 0:
str1 += 'U' * (now_i - i)
now_i = i
# Which column
for j, value in enumerate(board[i]):
if value == char:
if j - now_j > 0:
str1 += 'R' * (j - now_j)
if j - now_j < 0:
str1 += 'L' * (now_j - j)
str1 += '!'
now_j = j
return str1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution:
def alphabetBoardPath(self, target: str) -> str:
i = j = 0
ans = []
for char in target:
v = ord(char) - ord("a")
x, y = v // 5, v % 5
while j > y:
j -= 1
ans.append("L")
while i > x:
i -= 1
ans.append("U")
while j < y:
j += 1
ans.append("R")
while i < x:
i += 1
ans.append("D")
ans.append("!")
return "".join(ans)
  • Python
  • answer

扫一扫,分享到微信

微信分享二维码
1124. The longest period of performance One question daily
1139. The greatest 1 Formation of the border One question daily
目录
  1. 1. topic:
  2. 2. Thought:
  3. 3. Code:

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