Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 더현대서울 맛집
- Docker
- 금융문자분석경진대회
- 캐치카페
- Git
- Real or Not? NLP with Disaster Tweets
- 우분투
- PYTHON
- 코로나19
- leetcode
- 자연어처리
- ubuntu
- programmers
- Baekjoon
- SW Expert Academy
- github
- dacon
- 편스토랑 우승상품
- 프로그래머스
- 맥북
- 파이썬
- 편스토랑
- hackerrank
- 프로그래머스 파이썬
- Kaggle
- AI 경진대회
- 백준
- gs25
- ChatGPT
- 데이콘
Archives
- Today
- Total
솜씨좋은장씨
[leetCode] 6. ZigZag Conversion (Python) 본문
728x90
반응형
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P A H N
A P L S I I G
Y I R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
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
Solution
class Solution:
def convert(self, s: str, numRows: int) -> str:
max_interval = 2 * (numRows - 1)
string_len = len(s)
string_list = list(s)
new_string = []
if string_len == 1 or numRows == 1:
answer = s
else:
for i in range(numRows):
index_num = i
if (i == 0) or (i == numRows - 1):
while True:
if index_num > string_len-1:
break
new_string.append(string_list[index_num])
index_num = index_num + max_interval
else:
interval_first = 2 * (numRows - 1 - i)
interval_second = max_interval - interval_first
count = 0
while True:
if index_num > string_len-1:
break
new_string.append(string_list[index_num])
if count % 2 == 0:
index_num = index_num + interval_first
elif count % 2 == 1:
index_num = index_num + interval_second
count = count + 1
answer = ''.join(new_string)
return answer
'Programming > 코딩 1일 1문제' 카테고리의 다른 글
[BaekJoon] 10820번 : 문자열 분석 (Python) (2) | 2020.05.15 |
---|---|
[BaekJoon] 10845 : 큐 (Python) (0) | 2020.05.14 |
[BaekJoon] 1764번 : 듣보잡 (Python) (0) | 2020.05.12 |
[leetCode] 3. Longest Substring Without Repeating Characters (Python) (0) | 2020.05.11 |
[BaekJoon] 11053번 : 가장 긴 증가하는 부분수열 (Python) (0) | 2020.05.10 |
Comments