From c92d87680d7edff2f4542722793a788396e9d8f9 Mon Sep 17 00:00:00 2001 From: myoshizumi Date: Mon, 30 Jun 2025 17:17:11 +0900 Subject: [PATCH] leetcode 6. Zigzag Conversion --- .../at coder/Other/{ => atcoder}/A28/A28.js | 0 .../at coder/Other/{ => atcoder}/A36/A36.go | 0 .../at coder/Other/{ => atcoder}/A36/A36.java | 0 .../at coder/Other/{ => atcoder}/A36/A36.js | 0 .../at coder/Other/{ => atcoder}/A36/A36.php | 0 .../at coder/Other/{ => atcoder}/A36/A36.py | 0 .../Other/{ => atcoder}/A36/README.md | 0 .../at coder/Other/{ => atcoder}/A37/A37.go | 0 .../at coder/Other/{ => atcoder}/A37/A37.js | 0 .../at coder/Other/{ => atcoder}/A37/A37.php | 0 .../at coder/Other/{ => atcoder}/A37/A37.py | 0 .../Other/{ => atcoder}/A37/README.md | 0 .../at coder/Other/{ => atcoder}/A38/A38.go | 0 .../at coder/Other/{ => atcoder}/A38/A38.java | 0 .../at coder/Other/{ => atcoder}/A38/A38.js | 0 .../at coder/Other/{ => atcoder}/A38/A38.php | 0 .../at coder/Other/{ => atcoder}/A38/A38.py | 0 .../Other/{ => atcoder}/A38/README.md | 0 .../at coder/Other/{ => atcoder}/A40/A40.go | 0 .../at coder/Other/{ => atcoder}/A40/A40.java | 0 .../at coder/Other/{ => atcoder}/A40/A40.js | 0 .../at coder/Other/{ => atcoder}/A40/A40.php | 0 .../at coder/Other/{ => atcoder}/A40/A40.py | 0 .../Other/{ => atcoder}/A40/README.md | 0 .../at coder/Other/{ => atcoder}/A41/A41.js | 0 .../at coder/Other/{ => atcoder}/A41/A41.php | 0 .../Other/{ => atcoder}/A41/README.md | 0 .../at coder/Other/{ => atcoder}/A42/A42.go | 0 .../at coder/Other/{ => atcoder}/A42/A42.java | 0 .../at coder/Other/{ => atcoder}/A42/A42.js | 0 .../at coder/Other/{ => atcoder}/A42/A42.php | 0 .../at coder/Other/{ => atcoder}/A42/A42.py | 0 .../Other/{ => atcoder}/A42/README.md | 0 .../at coder/Other/{ => atcoder}/A43/A43.go | 0 .../at coder/Other/{ => atcoder}/A43/A43.java | 0 .../at coder/Other/{ => atcoder}/A43/A43.js | 0 .../at coder/Other/{ => atcoder}/A43/A43.php | 0 .../at coder/Other/{ => atcoder}/A43/A43.py | 0 .../Other/{ => atcoder}/A43/README.md | 0 .../at coder/Other/{ => atcoder}/A44/A44-1.js | 0 .../at coder/Other/{ => atcoder}/A44/A44-2.js | 0 .../at coder/Other/{ => atcoder}/A44/A44.go | 0 .../at coder/Other/{ => atcoder}/A44/A44.java | 0 .../at coder/Other/{ => atcoder}/A44/A44.php | 0 .../at coder/Other/{ => atcoder}/A44/A44.py | 0 .../Other/{ => atcoder}/A44/README.md | 0 .../at coder/Other/{ => atcoder}/A45/A45.js | 0 .../at coder/Other/{ => atcoder}/A45/A45.py | 0 .../at coder/Other/{ => atcoder}/A45/A45.ts | 0 .../Other/{ => atcoder}/A45/README.md | 0 .../leetcode/6. Zigzag Conversion/README.md | 82 +++++++++++++++++++ .../6. Zigzag Conversion/ZigzagConversion.ts | 20 +++++ 52 files changed, 102 insertions(+) rename Algorithm/Other/at coder/Other/{ => atcoder}/A28/A28.js (100%) rename Algorithm/Other/at coder/Other/{ => atcoder}/A36/A36.go (100%) rename Algorithm/Other/at coder/Other/{ => atcoder}/A36/A36.java (100%) rename Algorithm/Other/at coder/Other/{ => atcoder}/A36/A36.js (100%) rename Algorithm/Other/at coder/Other/{ => atcoder}/A36/A36.php (100%) rename Algorithm/Other/at coder/Other/{ => atcoder}/A36/A36.py (100%) rename Algorithm/Other/at coder/Other/{ => atcoder}/A36/README.md (100%) rename Algorithm/Other/at coder/Other/{ => atcoder}/A37/A37.go (100%) rename Algorithm/Other/at coder/Other/{ => atcoder}/A37/A37.js (100%) rename Algorithm/Other/at coder/Other/{ => atcoder}/A37/A37.php (100%) rename Algorithm/Other/at coder/Other/{ => atcoder}/A37/A37.py (100%) rename Algorithm/Other/at coder/Other/{ => atcoder}/A37/README.md (100%) rename Algorithm/Other/at coder/Other/{ => atcoder}/A38/A38.go (100%) rename Algorithm/Other/at coder/Other/{ => atcoder}/A38/A38.java (100%) rename Algorithm/Other/at coder/Other/{ => atcoder}/A38/A38.js (100%) rename Algorithm/Other/at coder/Other/{ => atcoder}/A38/A38.php (100%) rename Algorithm/Other/at coder/Other/{ => atcoder}/A38/A38.py (100%) rename Algorithm/Other/at coder/Other/{ => atcoder}/A38/README.md (100%) rename Algorithm/Other/at coder/Other/{ => atcoder}/A40/A40.go (100%) rename Algorithm/Other/at coder/Other/{ => atcoder}/A40/A40.java (100%) rename Algorithm/Other/at coder/Other/{ => atcoder}/A40/A40.js (100%) rename Algorithm/Other/at coder/Other/{ => atcoder}/A40/A40.php (100%) rename Algorithm/Other/at coder/Other/{ => atcoder}/A40/A40.py (100%) rename Algorithm/Other/at coder/Other/{ => atcoder}/A40/README.md (100%) rename Algorithm/Other/at coder/Other/{ => atcoder}/A41/A41.js (100%) rename Algorithm/Other/at coder/Other/{ => atcoder}/A41/A41.php (100%) rename Algorithm/Other/at coder/Other/{ => atcoder}/A41/README.md (100%) rename Algorithm/Other/at coder/Other/{ => atcoder}/A42/A42.go (100%) rename Algorithm/Other/at coder/Other/{ => atcoder}/A42/A42.java (100%) rename Algorithm/Other/at coder/Other/{ => atcoder}/A42/A42.js (100%) rename Algorithm/Other/at coder/Other/{ => atcoder}/A42/A42.php (100%) rename Algorithm/Other/at coder/Other/{ => atcoder}/A42/A42.py (100%) rename Algorithm/Other/at coder/Other/{ => atcoder}/A42/README.md (100%) rename Algorithm/Other/at coder/Other/{ => atcoder}/A43/A43.go (100%) rename Algorithm/Other/at coder/Other/{ => atcoder}/A43/A43.java (100%) rename Algorithm/Other/at coder/Other/{ => atcoder}/A43/A43.js (100%) rename Algorithm/Other/at coder/Other/{ => atcoder}/A43/A43.php (100%) rename Algorithm/Other/at coder/Other/{ => atcoder}/A43/A43.py (100%) rename Algorithm/Other/at coder/Other/{ => atcoder}/A43/README.md (100%) rename Algorithm/Other/at coder/Other/{ => atcoder}/A44/A44-1.js (100%) rename Algorithm/Other/at coder/Other/{ => atcoder}/A44/A44-2.js (100%) rename Algorithm/Other/at coder/Other/{ => atcoder}/A44/A44.go (100%) rename Algorithm/Other/at coder/Other/{ => atcoder}/A44/A44.java (100%) rename Algorithm/Other/at coder/Other/{ => atcoder}/A44/A44.php (100%) rename Algorithm/Other/at coder/Other/{ => atcoder}/A44/A44.py (100%) rename Algorithm/Other/at coder/Other/{ => atcoder}/A44/README.md (100%) rename Algorithm/Other/at coder/Other/{ => atcoder}/A45/A45.js (100%) rename Algorithm/Other/at coder/Other/{ => atcoder}/A45/A45.py (100%) rename Algorithm/Other/at coder/Other/{ => atcoder}/A45/A45.ts (100%) rename Algorithm/Other/at coder/Other/{ => atcoder}/A45/README.md (100%) create mode 100644 Algorithm/Other/at coder/Other/leetcode/6. Zigzag Conversion/README.md create mode 100644 Algorithm/Other/at coder/Other/leetcode/6. Zigzag Conversion/ZigzagConversion.ts diff --git a/Algorithm/Other/at coder/Other/A28/A28.js b/Algorithm/Other/at coder/Other/atcoder/A28/A28.js similarity index 100% rename from Algorithm/Other/at coder/Other/A28/A28.js rename to Algorithm/Other/at coder/Other/atcoder/A28/A28.js diff --git a/Algorithm/Other/at coder/Other/A36/A36.go b/Algorithm/Other/at coder/Other/atcoder/A36/A36.go similarity index 100% rename from Algorithm/Other/at coder/Other/A36/A36.go rename to Algorithm/Other/at coder/Other/atcoder/A36/A36.go diff --git a/Algorithm/Other/at coder/Other/A36/A36.java b/Algorithm/Other/at coder/Other/atcoder/A36/A36.java similarity index 100% rename from Algorithm/Other/at coder/Other/A36/A36.java rename to Algorithm/Other/at coder/Other/atcoder/A36/A36.java diff --git a/Algorithm/Other/at coder/Other/A36/A36.js b/Algorithm/Other/at coder/Other/atcoder/A36/A36.js similarity index 100% rename from Algorithm/Other/at coder/Other/A36/A36.js rename to Algorithm/Other/at coder/Other/atcoder/A36/A36.js diff --git a/Algorithm/Other/at coder/Other/A36/A36.php b/Algorithm/Other/at coder/Other/atcoder/A36/A36.php similarity index 100% rename from Algorithm/Other/at coder/Other/A36/A36.php rename to Algorithm/Other/at coder/Other/atcoder/A36/A36.php diff --git a/Algorithm/Other/at coder/Other/A36/A36.py b/Algorithm/Other/at coder/Other/atcoder/A36/A36.py similarity index 100% rename from Algorithm/Other/at coder/Other/A36/A36.py rename to Algorithm/Other/at coder/Other/atcoder/A36/A36.py diff --git a/Algorithm/Other/at coder/Other/A36/README.md b/Algorithm/Other/at coder/Other/atcoder/A36/README.md similarity index 100% rename from Algorithm/Other/at coder/Other/A36/README.md rename to Algorithm/Other/at coder/Other/atcoder/A36/README.md diff --git a/Algorithm/Other/at coder/Other/A37/A37.go b/Algorithm/Other/at coder/Other/atcoder/A37/A37.go similarity index 100% rename from Algorithm/Other/at coder/Other/A37/A37.go rename to Algorithm/Other/at coder/Other/atcoder/A37/A37.go diff --git a/Algorithm/Other/at coder/Other/A37/A37.js b/Algorithm/Other/at coder/Other/atcoder/A37/A37.js similarity index 100% rename from Algorithm/Other/at coder/Other/A37/A37.js rename to Algorithm/Other/at coder/Other/atcoder/A37/A37.js diff --git a/Algorithm/Other/at coder/Other/A37/A37.php b/Algorithm/Other/at coder/Other/atcoder/A37/A37.php similarity index 100% rename from Algorithm/Other/at coder/Other/A37/A37.php rename to Algorithm/Other/at coder/Other/atcoder/A37/A37.php diff --git a/Algorithm/Other/at coder/Other/A37/A37.py b/Algorithm/Other/at coder/Other/atcoder/A37/A37.py similarity index 100% rename from Algorithm/Other/at coder/Other/A37/A37.py rename to Algorithm/Other/at coder/Other/atcoder/A37/A37.py diff --git a/Algorithm/Other/at coder/Other/A37/README.md b/Algorithm/Other/at coder/Other/atcoder/A37/README.md similarity index 100% rename from Algorithm/Other/at coder/Other/A37/README.md rename to Algorithm/Other/at coder/Other/atcoder/A37/README.md diff --git a/Algorithm/Other/at coder/Other/A38/A38.go b/Algorithm/Other/at coder/Other/atcoder/A38/A38.go similarity index 100% rename from Algorithm/Other/at coder/Other/A38/A38.go rename to Algorithm/Other/at coder/Other/atcoder/A38/A38.go diff --git a/Algorithm/Other/at coder/Other/A38/A38.java b/Algorithm/Other/at coder/Other/atcoder/A38/A38.java similarity index 100% rename from Algorithm/Other/at coder/Other/A38/A38.java rename to Algorithm/Other/at coder/Other/atcoder/A38/A38.java diff --git a/Algorithm/Other/at coder/Other/A38/A38.js b/Algorithm/Other/at coder/Other/atcoder/A38/A38.js similarity index 100% rename from Algorithm/Other/at coder/Other/A38/A38.js rename to Algorithm/Other/at coder/Other/atcoder/A38/A38.js diff --git a/Algorithm/Other/at coder/Other/A38/A38.php b/Algorithm/Other/at coder/Other/atcoder/A38/A38.php similarity index 100% rename from Algorithm/Other/at coder/Other/A38/A38.php rename to Algorithm/Other/at coder/Other/atcoder/A38/A38.php diff --git a/Algorithm/Other/at coder/Other/A38/A38.py b/Algorithm/Other/at coder/Other/atcoder/A38/A38.py similarity index 100% rename from Algorithm/Other/at coder/Other/A38/A38.py rename to Algorithm/Other/at coder/Other/atcoder/A38/A38.py diff --git a/Algorithm/Other/at coder/Other/A38/README.md b/Algorithm/Other/at coder/Other/atcoder/A38/README.md similarity index 100% rename from Algorithm/Other/at coder/Other/A38/README.md rename to Algorithm/Other/at coder/Other/atcoder/A38/README.md diff --git a/Algorithm/Other/at coder/Other/A40/A40.go b/Algorithm/Other/at coder/Other/atcoder/A40/A40.go similarity index 100% rename from Algorithm/Other/at coder/Other/A40/A40.go rename to Algorithm/Other/at coder/Other/atcoder/A40/A40.go diff --git a/Algorithm/Other/at coder/Other/A40/A40.java b/Algorithm/Other/at coder/Other/atcoder/A40/A40.java similarity index 100% rename from Algorithm/Other/at coder/Other/A40/A40.java rename to Algorithm/Other/at coder/Other/atcoder/A40/A40.java diff --git a/Algorithm/Other/at coder/Other/A40/A40.js b/Algorithm/Other/at coder/Other/atcoder/A40/A40.js similarity index 100% rename from Algorithm/Other/at coder/Other/A40/A40.js rename to Algorithm/Other/at coder/Other/atcoder/A40/A40.js diff --git a/Algorithm/Other/at coder/Other/A40/A40.php b/Algorithm/Other/at coder/Other/atcoder/A40/A40.php similarity index 100% rename from Algorithm/Other/at coder/Other/A40/A40.php rename to Algorithm/Other/at coder/Other/atcoder/A40/A40.php diff --git a/Algorithm/Other/at coder/Other/A40/A40.py b/Algorithm/Other/at coder/Other/atcoder/A40/A40.py similarity index 100% rename from Algorithm/Other/at coder/Other/A40/A40.py rename to Algorithm/Other/at coder/Other/atcoder/A40/A40.py diff --git a/Algorithm/Other/at coder/Other/A40/README.md b/Algorithm/Other/at coder/Other/atcoder/A40/README.md similarity index 100% rename from Algorithm/Other/at coder/Other/A40/README.md rename to Algorithm/Other/at coder/Other/atcoder/A40/README.md diff --git a/Algorithm/Other/at coder/Other/A41/A41.js b/Algorithm/Other/at coder/Other/atcoder/A41/A41.js similarity index 100% rename from Algorithm/Other/at coder/Other/A41/A41.js rename to Algorithm/Other/at coder/Other/atcoder/A41/A41.js diff --git a/Algorithm/Other/at coder/Other/A41/A41.php b/Algorithm/Other/at coder/Other/atcoder/A41/A41.php similarity index 100% rename from Algorithm/Other/at coder/Other/A41/A41.php rename to Algorithm/Other/at coder/Other/atcoder/A41/A41.php diff --git a/Algorithm/Other/at coder/Other/A41/README.md b/Algorithm/Other/at coder/Other/atcoder/A41/README.md similarity index 100% rename from Algorithm/Other/at coder/Other/A41/README.md rename to Algorithm/Other/at coder/Other/atcoder/A41/README.md diff --git a/Algorithm/Other/at coder/Other/A42/A42.go b/Algorithm/Other/at coder/Other/atcoder/A42/A42.go similarity index 100% rename from Algorithm/Other/at coder/Other/A42/A42.go rename to Algorithm/Other/at coder/Other/atcoder/A42/A42.go diff --git a/Algorithm/Other/at coder/Other/A42/A42.java b/Algorithm/Other/at coder/Other/atcoder/A42/A42.java similarity index 100% rename from Algorithm/Other/at coder/Other/A42/A42.java rename to Algorithm/Other/at coder/Other/atcoder/A42/A42.java diff --git a/Algorithm/Other/at coder/Other/A42/A42.js b/Algorithm/Other/at coder/Other/atcoder/A42/A42.js similarity index 100% rename from Algorithm/Other/at coder/Other/A42/A42.js rename to Algorithm/Other/at coder/Other/atcoder/A42/A42.js diff --git a/Algorithm/Other/at coder/Other/A42/A42.php b/Algorithm/Other/at coder/Other/atcoder/A42/A42.php similarity index 100% rename from Algorithm/Other/at coder/Other/A42/A42.php rename to Algorithm/Other/at coder/Other/atcoder/A42/A42.php diff --git a/Algorithm/Other/at coder/Other/A42/A42.py b/Algorithm/Other/at coder/Other/atcoder/A42/A42.py similarity index 100% rename from Algorithm/Other/at coder/Other/A42/A42.py rename to Algorithm/Other/at coder/Other/atcoder/A42/A42.py diff --git a/Algorithm/Other/at coder/Other/A42/README.md b/Algorithm/Other/at coder/Other/atcoder/A42/README.md similarity index 100% rename from Algorithm/Other/at coder/Other/A42/README.md rename to Algorithm/Other/at coder/Other/atcoder/A42/README.md diff --git a/Algorithm/Other/at coder/Other/A43/A43.go b/Algorithm/Other/at coder/Other/atcoder/A43/A43.go similarity index 100% rename from Algorithm/Other/at coder/Other/A43/A43.go rename to Algorithm/Other/at coder/Other/atcoder/A43/A43.go diff --git a/Algorithm/Other/at coder/Other/A43/A43.java b/Algorithm/Other/at coder/Other/atcoder/A43/A43.java similarity index 100% rename from Algorithm/Other/at coder/Other/A43/A43.java rename to Algorithm/Other/at coder/Other/atcoder/A43/A43.java diff --git a/Algorithm/Other/at coder/Other/A43/A43.js b/Algorithm/Other/at coder/Other/atcoder/A43/A43.js similarity index 100% rename from Algorithm/Other/at coder/Other/A43/A43.js rename to Algorithm/Other/at coder/Other/atcoder/A43/A43.js diff --git a/Algorithm/Other/at coder/Other/A43/A43.php b/Algorithm/Other/at coder/Other/atcoder/A43/A43.php similarity index 100% rename from Algorithm/Other/at coder/Other/A43/A43.php rename to Algorithm/Other/at coder/Other/atcoder/A43/A43.php diff --git a/Algorithm/Other/at coder/Other/A43/A43.py b/Algorithm/Other/at coder/Other/atcoder/A43/A43.py similarity index 100% rename from Algorithm/Other/at coder/Other/A43/A43.py rename to Algorithm/Other/at coder/Other/atcoder/A43/A43.py diff --git a/Algorithm/Other/at coder/Other/A43/README.md b/Algorithm/Other/at coder/Other/atcoder/A43/README.md similarity index 100% rename from Algorithm/Other/at coder/Other/A43/README.md rename to Algorithm/Other/at coder/Other/atcoder/A43/README.md diff --git a/Algorithm/Other/at coder/Other/A44/A44-1.js b/Algorithm/Other/at coder/Other/atcoder/A44/A44-1.js similarity index 100% rename from Algorithm/Other/at coder/Other/A44/A44-1.js rename to Algorithm/Other/at coder/Other/atcoder/A44/A44-1.js diff --git a/Algorithm/Other/at coder/Other/A44/A44-2.js b/Algorithm/Other/at coder/Other/atcoder/A44/A44-2.js similarity index 100% rename from Algorithm/Other/at coder/Other/A44/A44-2.js rename to Algorithm/Other/at coder/Other/atcoder/A44/A44-2.js diff --git a/Algorithm/Other/at coder/Other/A44/A44.go b/Algorithm/Other/at coder/Other/atcoder/A44/A44.go similarity index 100% rename from Algorithm/Other/at coder/Other/A44/A44.go rename to Algorithm/Other/at coder/Other/atcoder/A44/A44.go diff --git a/Algorithm/Other/at coder/Other/A44/A44.java b/Algorithm/Other/at coder/Other/atcoder/A44/A44.java similarity index 100% rename from Algorithm/Other/at coder/Other/A44/A44.java rename to Algorithm/Other/at coder/Other/atcoder/A44/A44.java diff --git a/Algorithm/Other/at coder/Other/A44/A44.php b/Algorithm/Other/at coder/Other/atcoder/A44/A44.php similarity index 100% rename from Algorithm/Other/at coder/Other/A44/A44.php rename to Algorithm/Other/at coder/Other/atcoder/A44/A44.php diff --git a/Algorithm/Other/at coder/Other/A44/A44.py b/Algorithm/Other/at coder/Other/atcoder/A44/A44.py similarity index 100% rename from Algorithm/Other/at coder/Other/A44/A44.py rename to Algorithm/Other/at coder/Other/atcoder/A44/A44.py diff --git a/Algorithm/Other/at coder/Other/A44/README.md b/Algorithm/Other/at coder/Other/atcoder/A44/README.md similarity index 100% rename from Algorithm/Other/at coder/Other/A44/README.md rename to Algorithm/Other/at coder/Other/atcoder/A44/README.md diff --git a/Algorithm/Other/at coder/Other/A45/A45.js b/Algorithm/Other/at coder/Other/atcoder/A45/A45.js similarity index 100% rename from Algorithm/Other/at coder/Other/A45/A45.js rename to Algorithm/Other/at coder/Other/atcoder/A45/A45.js diff --git a/Algorithm/Other/at coder/Other/A45/A45.py b/Algorithm/Other/at coder/Other/atcoder/A45/A45.py similarity index 100% rename from Algorithm/Other/at coder/Other/A45/A45.py rename to Algorithm/Other/at coder/Other/atcoder/A45/A45.py diff --git a/Algorithm/Other/at coder/Other/A45/A45.ts b/Algorithm/Other/at coder/Other/atcoder/A45/A45.ts similarity index 100% rename from Algorithm/Other/at coder/Other/A45/A45.ts rename to Algorithm/Other/at coder/Other/atcoder/A45/A45.ts diff --git a/Algorithm/Other/at coder/Other/A45/README.md b/Algorithm/Other/at coder/Other/atcoder/A45/README.md similarity index 100% rename from Algorithm/Other/at coder/Other/A45/README.md rename to Algorithm/Other/at coder/Other/atcoder/A45/README.md diff --git a/Algorithm/Other/at coder/Other/leetcode/6. Zigzag Conversion/README.md b/Algorithm/Other/at coder/Other/leetcode/6. Zigzag Conversion/README.md new file mode 100644 index 00000000..471e793c --- /dev/null +++ b/Algorithm/Other/at coder/Other/leetcode/6. Zigzag Conversion/README.md @@ -0,0 +1,82 @@ + +```ts +function convert(s: string, numRows: number): string { + if (numRows === 1 || s.length <= numRows) return s; + + const rows: string[] = Array(Math.min(numRows, s.length)).fill(""); + let currentRow = 0; + let goingDown = false; + + for (const char of s) { + rows[currentRow] += char; + + // 一番上または一番下に到達したら方向を反転 + if (currentRow === 0 || currentRow === numRows - 1) { + goingDown = !goingDown; + } + + currentRow += goingDown ? 1 : -1; + } + + return rows.join(""); +} +``` + +--- + +### 🔍 実行例: + +#### 入力1: + +```ts +console.log(convert("PAYPALISHIRING", 3)); +``` + +出力: + +``` +"PAHNAPLSIIGYIR" +``` + +#### 入力2: + +```ts +console.log(convert("PAYPALISHIRING", 4)); +``` + +出力: + +``` +"PINALSIGYAHRPI" +``` + +#### 入力3: + +```ts +console.log(convert("A", 1)); +``` + +出力: + +``` +"A" +``` + +--- + +### 🧠 アルゴリズムの考え方(図付き) + +例: `s = "PAYPALISHIRING", numRows = 4` + +``` +P I N +A L S I G +Y A H R +P I +``` + +* 各文字を1文字ずつ `rows[]` に追加 +* 上から下に移動し、最下段に達したら反転して上に戻るというパターン +* 方向はフラグ `goingDown` で管理 + +--- \ No newline at end of file diff --git a/Algorithm/Other/at coder/Other/leetcode/6. Zigzag Conversion/ZigzagConversion.ts b/Algorithm/Other/at coder/Other/leetcode/6. Zigzag Conversion/ZigzagConversion.ts new file mode 100644 index 00000000..69ceba7d --- /dev/null +++ b/Algorithm/Other/at coder/Other/leetcode/6. Zigzag Conversion/ZigzagConversion.ts @@ -0,0 +1,20 @@ +function convert(s: string, numRows: number): string { + if (numRows === 1 || s.length <= numRows) return s; + + const rows: string[] = Array(Math.min(numRows, s.length)).fill(''); + let currentRow = 0; + let goingDown = false; + + for (const char of s) { + rows[currentRow] += char; + + // 一番上または一番下に到達したら方向を反転 + if (currentRow === 0 || currentRow === numRows - 1) { + goingDown = !goingDown; + } + + currentRow += goingDown ? 1 : -1; + } + + return rows.join(''); +}