Commit 743379c
committed
6. Zigzag Conversion
```Solution.c char* convert(char* s, int numRows) { int sLength = strlen(s); char *res = (char *)malloc((sLength + 1) * sizeof(char)); res[sLength] = '\0'; int resPos = 0; int i = 0; int gap = (numRows - 1) * 2; if(gap == 0) { gap = 1; } for(int j = 0; j < sLength; j+=gap) { res[resPos] = s[j]; resPos++; } int gapLeft = gap; for(i=1; i<numRows-1; i++) { gapLeft-=2; int currGap = gap - gapLeft; for(int j = i; j < sLength; j+= currGap){ res[resPos] = s[j]; resPos++; currGap = gap - currGap; } } for(int j = i; j < sLength && numRows > 1; j+=gap) { res[resPos] = s[j]; resPos++; } return res; } ``` ```Solution.cpp class Solution { public: string convert(string s, int numRows) { if (numRows == 1 || s.length() <= numRows) return s; vector<string> rows(min(numRows, int(s.size()))); int curRow = 0, dir = -1; for (char c : s) { rows[curRow] += c; if (curRow == 0 || curRow == numRows - 1) dir *= -1; curRow += dir; } string ret; for (string row : rows) ret += row; return ret; } }; ``` ```Solution.cs public class Solution { public string Convert(string s, int numRows) { if (numRows == 1 || s.Length <= numRows) return s; var rows = new List<string>(new string[Math.Min(numRows, s.Length)]); int curRow = 0; bool goingDown = false; foreach (char c in s) { rows[curRow] += c; if (curRow == 0 || curRow == numRows - 1) goingDown = !goingDown; curRow += goingDown ? 1 : -1; } return string.Join("", rows); } } ``` ```Solution.dart class Solution { String convert(String s, int numRows) { if (numRows == 1 || s.length <= numRows) return s; List<StringBuffer> rows = List.generate(numRows, (_) => StringBuffer()); int curRow = 0; bool goingDown = false; for (var c in s.split('')) { rows[curRow].write(c); if (curRow == 0 || curRow == numRows - 1) goingDown = !goingDown; curRow += goingDown ? 1 : -1; } return rows.map((sb) => sb.toString()).join(''); } } ``` ```Solution.erl -spec convert(S :: unicode:unicode_binary(), NumRows :: integer()) -> unicode:unicode_binary(). convert(S, NumRows) when NumRows =< 1; NumRows >= byte_size(S) -> S; convert(S, NumRows) -> Rows = lists:duplicate(NumRows, []), convert(S, NumRows, 1, 1, Rows). convert(<<>>, _NumRows, _CurRow, _Dir, Rows) -> unicode:characters_to_binary(lists:flatten(lists:map(fun lists:reverse/1, Rows))); convert(<<C/utf8, Rest/binary>>, NumRows, CurRow, Dir, Rows) -> NewRows = add_char(Rows, CurRow, C), NewDir = case {CurRow, Dir} of {1, -1} -> 1; {NumRows, 1} -> -1; _ -> Dir end, NewCurRow = CurRow + NewDir, convert(Rest, NumRows, NewCurRow, NewDir, NewRows). add_char(Rows, CurRow, C) -> {NewRows, _} = lists:mapfoldl( fun(Row, Idx) -> if Idx =:= CurRow -> {[C | Row], Idx + 1}; true -> {Row, Idx + 1} end end, 1, Rows), NewRows. ``` ```Solution.ex defmodule Solution do @SPEC convert(s :: String.t, num_rows :: integer) :: String.t def convert(s, 1), do: s def convert(s, num_rows) do cycle_len = 2 * num_rows - 2 0..(num_rows - 1) |> Enum.map(fn row -> add_row(s, num_rows, cycle_len, row) end) |> IO.iodata_to_binary() end def add_row(s, num_rows, cycle_len, row), do: add_row(s, num_rows, cycle_len, row, 0, []) def add_row(s, num_rows, cycle_len, row, cycle, acc) when row + cycle * cycle_len < byte_size(s) do index = row + cycle * cycle_len acc = [:binary.at(s, index) | acc] acc = if row > 0 and row < num_rows - 1 and (cycle + 1) * cycle_len - row < byte_size(s) do index = (cycle + 1) * cycle_len - row [:binary.at(s, index) | acc] else acc end add_row(s, num_rows, cycle_len, row, cycle + 1, acc) end def add_row(_, _, _, _, _, acc), do: Enum.reverse(acc) end ``` ```Solution.go func convert(s string, numRows int) string { if numRows == 1 || len(s) <= numRows { return s } rows := make([]string, min(numRows, len(s))) curRow := 0 goingDown := false for _, c := range s { rows[curRow] += string(c) if curRow == 0 || curRow == numRows-1 { goingDown = !goingDown } if goingDown { curRow++ } else { curRow-- } } return strings.Join(rows, "") } ``` ```Solution.java class Solution { public String convert(String s, int numRows) { if (numRows == 1 || s.length() <= numRows) return s; StringBuilder[] rows = new StringBuilder[Math.min(numRows, s.length())]; for (int i = 0; i < rows.length; i++) rows[i] = new StringBuilder(); int curRow = 0, dir = -1; for (char c : s.toCharArray()) { rows[curRow].append(c); if (curRow == 0 || curRow == numRows - 1) dir *= -1; curRow += dir; } StringBuilder result = new StringBuilder(); for (StringBuilder row : rows) result.append(row); return result.toString(); } } ``` ```Solution.js /** * @param {string} s * @param {number} numRows * @return {string} */ var convert = function(s, numRows) { if (numRows === 1 || s.length <= numRows) return s; let rows = Array(Math.min(numRows, s.length)).fill(''); let curRow = 0, goingDown = false; for (let c of s) { rows[curRow] += c; if (curRow === 0 || curRow === numRows - 1) goingDown = !goingDown; curRow += goingDown ? 1 : -1; } return rows.join(''); }; ``` ```Solution.kt class Solution { fun convert(s: String, numRows: Int): String { if (numRows == 1 || s.length <= numRows) return s val rows = Array(minOf(numRows, s.length)) { StringBuilder() } var curRow = 0 var goingDown = false for (c in s) { rows[curRow].append(c) if (curRow == 0 || curRow == numRows - 1) goingDown = !goingDown curRow += if (goingDown) 1 else -1 } return rows.joinToString("") { it.toString() } } } ``` ```Solution.php class Solution { /** * @param String $s * @param Integer $numRows * @return String */ function convert($s, $numRows) { if ($numRows == 1 || strlen($s) <= $numRows) { return $s; } $rows = array_fill(0, $numRows, ""); $curRow = 0; $down = false; for ($i = 0; $i < strlen($s); $i++) { $rows[$curRow] .= $s[$i]; if ($curRow == 0 || $curRow == $numRows - 1) { $down = !$down; } $curRow += $down ? 1 : -1; } return implode("", $rows); } } ``` ```Solution.py class Solution(object): def convert(self, s, numRows): if numRows == 1 or len(s) <= numRows: return s rows = [''] * min(numRows, len(s)) cur_row, going_down = 0, False for c in s: rows[cur_row] += c if cur_row == 0 or cur_row == numRows - 1: going_down = not going_down cur_row += 1 if going_down else -1 return ''.join(rows) ``` ```Solution.rb # @param {String} s # @param {Integer} num_rows # @return {String} def convert(s, num_rows) return s if num_rows == 1 || s.length <= num_rows rows = Array.new([num_rows, s.length].min, "") cur_row, going_down = 0, false s.each_char do |c| rows[cur_row] += c going_down = !going_down if cur_row == 0 || cur_row == num_rows - 1 cur_row += going_down ? 1 : -1 end rows.join end ``` ```Solution.rkt (define (convert s numRows) (if (or (= numRows 1) (<= (string-length s) numRows)) s (let loop ((chars (string->list s)) (rows (make-vector numRows "")) (cur 0) (dir 1)) (if (null? chars) (apply string-append (vector->list rows)) (begin (vector-set! rows cur (string-append (vector-ref rows cur) (string (car chars)))) (let ((new-dir (cond ((= cur 0) 1) ((= cur (- numRows 1)) -1) (else dir)))) (loop (cdr chars) rows (+ cur new-dir) new-dir))))))) ``` ```Solution.rs impl Solution { pub fn convert(s: String, num_rows: i32) -> String { if num_rows == 1 || s.len() <= num_rows as usize { return s; } let mut rows = vec![String::new(); num_rows as usize]; let mut cur_row = 0; let mut down = false; for c in s.chars() { rows[cur_row].push(c); if cur_row == 0 || cur_row == (num_rows - 1) as usize { down = !down; } cur_row = if down { cur_row + 1 } else { cur_row - 1 }; } rows.concat() } } ``` ```Solution.scala object Solution { def convert(s: String, numRows: Int): String = { if (numRows == 1 || s.length <= numRows) return s val rows = Array.fill(Math.min(numRows, s.length))("") var curRow = 0 var goingDown = false for (c <- s) { rows(curRow) += c if (curRow == 0 || curRow == numRows - 1) goingDown = !goingDown curRow += (if (goingDown) 1 else -1) } rows.mkString("") } } ``` ```Solution.swift class Solution { func convert(_ s: String, _ numRows: Int) -> String { if numRows == 1 || s.count <= numRows { return s } var rows = Array(repeating: "", count: min(numRows, s.count)) var curRow = 0 var goingDown = false for c in s { rows[curRow] += String(c) if curRow == 0 || curRow == numRows - 1 { goingDown.toggle() } curRow += goingDown ? 1 : -1 } return rows.joined() } } ``` ```Solution.ts function convert(s: string, numRows: number): string { if (numRows === 1 || s.length <= numRows) return s; let rows: string[] = Array(Math.min(numRows, s.length)).fill(""); let curRow = 0; let goingDown = false; for (let c of s) { rows[curRow] += c; if (curRow === 0 || curRow === numRows - 1) goingDown = !goingDown; curRow += goingDown ? 1 : -1; } return rows.join(""); } ```1 parent 06a6cd2 commit 743379c
File tree
19 files changed
+353
-0
lines changed- sol/solution/0001-0100/0006
19 files changed
+353
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 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 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 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 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 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 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 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 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
0 commit comments