๐ ๋ฌธ์
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);
๐ ์์
Input: s = "PAYPALISHIRING", numRows = 3
Output: "PAHNAPLSIIGYIR"
Input: s = "PAYPALISHIRING", numRows = 4
Output: "PINALSIGYAHRPI"
Explanation:
P I N
A L S I G
Y A H R
P I
๐ ๋ด๊ฐ ํผ ๋ฐฉ๋ฒ ์ค๋ช
์ด์ฐจ์ ๋ฐฐ์ด์ด ์๋, ์ด์ฐจ์ ๋ฐฐ์ด ๋ฆฌ์คํธ๋ฅผ ์ฌ์ฉํ ์ด์
์ฒ์์๋ ์ด์ฐจ์ ๋ฐฐ์ด ํ์์ผ๋ก ํ๋ ค๊ณ ํ์ผ๋, ์ด์ ํฌ๊ธฐ๋ฅผ ์ ์ ์์ด ๊ฐ๋ณ ๋ฐฐ์ด์ธ ArrayList๋ฅผ ์ฌ์ฉํ์๋ค.
์ด์ฐจ์ ArrayList๋ฅผ ๋ง๋ค์ด์ฃผ๊ณ , ํ์ ํฌ๊ธฐ๋ numRows ์ค์ ํด์ฃผ์๋ค.
ArrayList๋ ์ด๊ธฐํ ํด์ฃผ๊ณ ์์ํ์๋ค.
๋ง์ผ ์ด๊ธฐํ ํด์ฃผ์ง ์์ผ๋ฉด, ํด๋น ํ ๋ณ๋ก ArrayList๊ฐ ์กด์ฌํ์ง ์๊ธฐ ๋๋ฌธ์ NPE๊ฐ ๋ฐ์ํ๋ค.
public static String convert(String s, int numRows) {
if (numRows == 1) {
return s;
}
ArrayList<String>[] arr = new ArrayList[numRows];
for (int i = 0; i < numRows; i++) {
arr[i] = new ArrayList<>();
}
}
ํ์ด
์์ํ๋ ํ์ ๊ฐ์ 0์ผ๋ก, ์์ํ๋ ๋ฌธ์์ด์ ์ธ๋ฑ์ค ๊ฐ์ 0์ผ๋ก ์ด๊ธฐํ ํด์ฃผ์๋ค.
๊ฐ ํ์ ์ฒซ ์ด์๋ ๊ฐ ๋ฌธ์๊ฐ ๋ค ๋ค์ด๊ฐ๋ฏ๋ก, ํ๋์ฉ ๋ค ๋ฃ์ด์ฃผ์๋ค.
์๋ ์ฌ์ง์ ์ฐธ๊ณ ํ๋ฉด, ์ง๊ทธ์ฌ๊ทธ๋ก ๋ฌธ์๊ฐ ์ด๋ป๊ฒ ๋ค์ด๊ฐ๋์ง ํ์ธ ํ ์ ์๋ค.๋๋ฌธ์ ํ์ ๊ฐ์ -2 ์์ผ์ฃผ์๋ค.์๋ํ๋ฉด, ์ด๋ฏธ ๋ฐ๋ณต๋ฌธ ์์ ๋ฐ๋ณต๋ฌธ์์ ํ์ ๊ฐ์ ++์์ผ์ฃผ์๊ธฐ ๋๋ฌธ์ด๋ค.
๊ทธ๋ฆฌ๊ณ ํด๋น ํ ๋ถํฐ -1์ฉ ๊ฐ์์ํค๋ฉฐ ๋ฌธ์๋ค์ ๋ฃ์ด์ฃผ์๋ค.
๋์ค์ ๋ฌธ์์ด ํํ๋ก ์ถ๋ ฅํ ์์ ์ด๋ฉฐ, ๊ฐ ํ๋ณ๋ก ํ๊ฐ์ฉ ๋ค์ด๊ฐ๊ธฐ ๋๋ฌธ์ ์ด๋ ๊ฒ ๋ฃ์ด์ค๋ ์๊ด ์๋ค๊ณ ํ๋จํ์๋ค.
public static String convert(String s, int numRows) {
...
int curRow = 0;
int curStringIdx = 0;
while(curStringIdx < s.length()) {
while (curRow < numRows && curStringIdx < s.length()) {
arr[curRow].add(String.valueOf(s.charAt(curStringIdx)));
curRow++;
curStringIdx++;
}
curRow -= 2;
while (curRow > 0 && curStringIdx < s.length()) {
arr[curRow].add(String.valueOf(s.charAt(curStringIdx)));
curRow--;
curStringIdx++;
}
}
...
}
์ถ๋ ฅ
StringBuilder๋ฅผ ์ฌ์ฉํ์ฌ ํ๋์ฉ ๋ฌธ์๋ฅผ ์ถ๋ ฅํด์ฃผ์๋ค.
๋ฐฐ์ด ๋ฆฌ์คํธ๋ฅผ ์ด ๋งํผ ์คํธ๋ฆผ์ ์๋ํด ๋ณด์์ผ๋, ๋ฐฐ์ด ๋ฆฌ์คํธ ์์ ๋ฐฐ์ด์ด๋ผ ์ถ๋ ฅ์ด ์ด๋ ค์ ๋ค.๊ทธ๋์ ๋ฐ๋ณต๋ฌธ์ ๋๋ ค ํ๋์ฉ ์ถ๋ ฅํด์ฃผ์๋ค.
public static String convert(String s, int numRows) {
...
StringBuilder sb = new StringBuilder();
for (int i = 0; i < numRows; i++) {
for (int j = 0 ; j < arr[i].size(); j++) {
sb.append(arr[i].get(j));
}
}
return sb.toString();
}
๐ ์ต์ข ํ์ด
public class ZigzagConversion {
public static void main(String[] args) {
System.out.println(convert("PAYPALISHIRING", 3));
System.out.println(convert("PAYPALISHIRING", 4));
}
public static String convert(String s, int numRows) {
if (numRows == 1) {
return s;
}
ArrayList<String>[] arr = new ArrayList[numRows];
int curRow = 0;
int curStringIdx = 0;
for (int i = 0; i < numRows; i++) {
arr[i] = new ArrayList<>();
}
while(curStringIdx < s.length()) {
while (curRow < numRows && curStringIdx < s.length()) {
arr[curRow].add(String.valueOf(s.charAt(curStringIdx)));
curRow++;
curStringIdx++;
}
curRow -= 2;
while (curRow > 0 && curStringIdx < s.length()) {
arr[curRow].add(String.valueOf(s.charAt(curStringIdx)));
curRow--;
curStringIdx++;
}
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < numRows; i++) {
for (int j = 0 ; j < arr[i].size(); j++) {
sb.append(arr[i].get(j));
}
}
return sb.toString();
}
}
'์๊ณ ๋ฆฌ์ฆ > LeetCode' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[LeetCode] 004. Median of Two Sorted Arrays JAVA (0) | 2022.12.22 |
---|---|
[LeetCode] 008. String to Integer (atoi) JAVA (0) | 2022.12.21 |
[LeetCode]007. Reverse Integer JAVA (0) | 2022.12.19 |
[LeetCode]005. Longest Palindromic Substring JAVA (0) | 2022.12.19 |
[LeetCode] 003. Longest Substring Without Repeating Characters JAVA (0) | 2022.12.16 |
๋๊ธ