It seems that coding tests are conducted overseas in interviews with engineers, and in many cases, the main thing is to implement specific functions and classes according to the theme.
Apparently, many engineers take measures on the site called LetCode.
It is a site that trains the algorithmic power that can withstand the coding test that is being done in the early story, and it is an inevitable path for those who want to build a career at an overseas tech company.
I wrote it in a big way, but I have no plans to have such an interview at the moment.
However, as an IT engineer, it would be better to have the same level of algorithm power as a person, so I would like to solve the problem irregularly and write down the method I thought at that time as a memo.
I'm solving it with Python3.
Leet Code Table of Contents Starting from Zero
Last time Leet Code Day 84 starting from zero "142. Linked List Cycle II"
Twitter I'm doing it.
** Technical Blog Started! !! ** ** I think the technology will write about LetCode, Django, Nuxt, and so on. ** This is faster to update **, so please bookmark it!
The difficulty level is Medium. Continuing from the last time, this is an excerpt from LeetCode 60 questions I want to solve for coding interview preparation.
The problem is that you are first given a string, for example the string PAYPALISHIRING
. These character strings are written in a zigzag pattern on a specified number of lines as shown below. (It's a good idea to display this pattern in a fixed font for readability)
P A H N
A P L S I I G
Y I R
If you read these line by line, you will get " PAHNAPLSIIGYIR "
.
Write the code that takes these strings and converts them by specifying the number of lines.
string convert(string s, int numRows).
Example 1:
Input: s = "PAYPALISHIRING", numRows = 3 Output: "PAHNAPLSIIGYIR"
Example 2:
Input: s = "PAYPALISHIRING", numRows = 4 Output: "PINALSIGYAHRPI" Explanation:
P I N
A L S I G
Y A H R
P I
By the way, I cloned the list of problem books, but the problem I solved was 23/60 at this point.
Like this ↓
It's important to have fun solving, so I'd like to answer everything within a reasonable range. Oh, and there are some problems that can't be solved without subscribing. In the first place, even if I subscribe, I will skip it because it is a right to post a problem.
class Solution:
def convert(self, s: str, numRows: int) -> str:
if numRows == 1 or numRows >= len(s):
return s
num,step,ans = 0,0,['']*numRows
for w in s:
ans[num] += w
if num == 0:
step = 1
elif num == numRows - 1:
step = -1
num += step
return "".join(ans)
# Runtime: 64 ms, faster than 65.13% of Python3 online submissions for ZigZag Conversion.
# Memory Usage: 14.1 MB, less than 10.13% of Python3 online submissions for ZigZag Conversion.
Prepare as many arrays as there are numRows
and add each one. By managing the increase / decrease with step
after adding, for example, in the test case
PAYPALISHIRING
In this case, you can see that it can be inserted into the array as shown below.
['PAHN', 'APLSIIG', 'YIR']
If you finally do `` join () with
'', the
" PAHNAPLSIIGYIR "` of str will be returned safely, and so on.
This is 24/60. So that's it for this time. Thank you for your hard work.
Recommended Posts