๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์•Œ๊ณ ๋ฆฌ์ฆ˜/LeetCode

[LeetCode]006. Zigzag Conversion JAVA

by Bhinney 2022. 12. 21.
 

Zigzag Conversion - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com


๐Ÿ“Œ ๋ฌธ์ œ

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();
   }
}

 

๋Œ“๊ธ€