Implementation of the ZigZag Conversion Leetcode problem.
More...
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
|
char * | convert (char *in, uint16_t numRows) |
| for assert for unsigned int with fixed size More...
|
|
static void | testZigZag (char *s, int numRows, char *expected) |
| Self-test implementations. More...
|
|
static void | test () |
| Self-test implementations. More...
|
|
int | main (void) |
| Main function. More...
|
|
Implementation of the ZigZag Conversion Leetcode problem.
A decent solution to the ZigZag conversion problem. Take advantage of the fact that the maximum gap between the chars is 2 times the depth(the number of rows). The actual gap between the two first chars of a rows depends on the depth of the row. The gaps between successives chars on the same row is the complement of the first gap to the maximum gap.
- Author
- straight_into_the_wall
◆ convert()
char * convert |
( |
char * |
in, |
|
|
uint16_t |
numRows |
|
) |
| |
for assert for unsigned int with fixed size
for IO operations for malloc for string tools
Convert a string to the it's zigzag equivalent on a given number of rows.
- Parameters
-
in | the string in input. |
numRows | the desired number of rows. |
- Returns
- the converted new (malloced) string.
31 uint16_t len = strlen(in);
37 char* out =
calloc(len + 1,
sizeof(
char));
41 memcpy(out, in, len + 1);
45 uint16_t
max = numRows - 1;
46 uint16_t rr = 2 *
max;
59 for (uint16_t l = 1; l <
max; l++)
#define max(a, b)
shorthand for maximum value
Definition: kohonen_som_topology.c:39
#define calloc(elemCount, elemSize)
This macro replace the standard calloc function with calloc_dbg.
Definition: malloc_dbg.h:22
◆ main()
Main function.
- Returns
- 0 on exit
static void test()
Self-test implementations.
Definition: 6.c:100
◆ test()
static void test |
( |
void |
| ) |
|
|
static |
Self-test implementations.
- Returns
- void
102 char* s01 =
"PAYPALISHIRING";
104 char* r01 =
"PINALSIGYAHRPI";
107 char* r02 =
"PAHNAPLSIIGYIR";
115 "cbxdwjccgtdoqiscyspqzvuqivzptlpvooynyapgvswoaosaghrffnxnjyeeltzaiznicc"
116 "ozwknwyhzgpqlwfkjqipuu"
117 "jvwtxlbznryjdohbvghmyuiggtyqjtmuqinntqmihntkddnalwnmsxsatqqeldacnnpjfe"
118 "rmrnyuqnwbjjpdjhdeavkn"
119 "ykpoxhxclqqedqavdwzoiorrwwxyrhlsrdgqkduvtmzzczufvtvfioygkvedervvudnegh"
120 "bctcbxdxezrzgbpfhzanff"
121 "eccbgqfmzjqtlrsppxqiywjobspefujlxnmddurddiyobqfspvcoulcvdrzkmkwlyiqdch"
122 "ghrgytzdnobqcvdeqjystm"
123 "epxcaniewqmoxkjwpymqorluxedvywhcoghotpusfgiestckrpaigocfufbubiyrrffmwa"
124 "eeimidfnnzcphkflpbqsvt"
125 "dwludsgaungfzoihbxifoprwcjzsdxngtacw";
128 "cbxdwjccgtdoqiscyspqzvuqivzptlpvooynyapgvswoaosaghrffnxnjyeeltzaiznicc"
129 "ozwknwyhzgpqlwfkjqipuu"
130 "jvwtxlbznryjdohbvghmyuiggtyqjtmuqinntqmihntkddnalwnmsxsatqqeldacnnpjfe"
131 "rmrnyuqnwbjjpdjhdeavkn"
132 "ykpoxhxclqqedqavdwzoiorrwwxyrhlsrdgqkduvtmzzczufvtvfioygkvedervvudnegh"
133 "bctcbxdxezrzgbpfhzanff"
134 "eccbgqfmzjqtlrsppxqiywjobspefujlxnmddurddiyobqfspvcoulcvdrzkmkwlyiqdch"
135 "ghrgytzdnobqcvdeqjystm"
136 "epxcaniewqmoxkjwpymqorluxedvywhcoghotpusfgiestckrpaigocfufbubiyrrffmwa"
137 "eeimidfnnzwccpahtkgfnl"
138 "xpdbsqzsjvctwdrwploufdisxgbahuinogzf";
static void testZigZag(char *s, int numRows, char *expected)
Self-test implementations.
Definition: 6.c:86
◆ testZigZag()
static void testZigZag |
( |
char * |
s, |
|
|
int |
numRows, |
|
|
char * |
expected |
|
) |
| |
|
static |
Self-test implementations.
- Returns
- void
88 char* ret =
convert(s, numRows);
90 int cmp = strncmp(ret, expected, len);
char * convert(char *in, uint16_t numRows)
for assert for unsigned int with fixed size
Definition: 6.c:29
#define free(ptr)
This macro replace the standard free function with free_dbg.
Definition: malloc_dbg.h:26