Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
Loading...
Searching...
No Matches
6.c File Reference

Implementation of the ZigZag Conversion Leetcode problem. More...

#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Include dependency graph for 6.c:

Functions

char * convert (char *in, uint16_t numRows)
 for assert for unsigned int with fixed size
 
static void testZigZag (char *s, int numRows, char *expected)
 Self-test implementations.
 
static void test ()
 Self-test implementations.
 
int main (void)
 Main function.
 

Detailed Description

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

Function Documentation

◆ 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
inthe string in input.
numRowsthe desired number of rows.
Returns
the converted new (malloced) string.
30{
31 uint16_t len = strlen(in);
32
33 if (len < numRows)
34 {
35 numRows = len;
36 }
37 char* out = calloc(len + 1, sizeof(char));
38
39 if (numRows < 2)
40 {
41 memcpy(out, in, len + 1);
42 return out;
43 }
44
45 uint16_t max = numRows - 1;
46 uint16_t rr = 2 * max;
47 uint16_t i = 0;
48 uint16_t o = 0;
49 uint16_t delta = 0;
50
51 // first row
52 while (i < len)
53 {
54 out[o++] = in[i];
55 i += rr;
56 }
57
58 // middle rows
59 for (uint16_t l = 1; l < max; l++)
60 {
61 i = l;
62 delta = 2 * l;
63 while (i < len)
64 {
65 out[o++] = in[i];
66 delta = rr - delta;
67 i += delta;
68 }
69 }
70
71 // last row
72 i = max;
73 while (i < len)
74 {
75 out[o++] = in[i];
76 i += rr;
77 }
78
79 return out;
80}
#define calloc(elemCount, elemSize)
This macro replace the standard calloc function with calloc_dbg.
Definition malloc_dbg.h:22

◆ main()

int main ( void  )

Main function.

Returns
0 on exit
148{
149 test(); // run self-test implementations
150 return 0;
151}
static void test()
Self-test implementations.
Definition 6.c:100
Here is the call graph for this function:

◆ test()

static void test ( void  )
static

Self-test implementations.

Returns
void
101{
102 char* s01 = "PAYPALISHIRING";
103
104 char* r01 = "PINALSIGYAHRPI";
105 testZigZag(s01, 4, r01);
106
107 char* r02 = "PAHNAPLSIIGYIR";
108 testZigZag(s01, 3, r02);
109
110 char* s03 = "A";
111 testZigZag(s03, 1, s03);
112 testZigZag(s03, 3, s03);
113
114 char* s04 =
115 "cbxdwjccgtdoqiscyspqzvuqivzptlpvooynyapgvswoaosaghrffnxnjyeeltzaiznicc"
116 "ozwknwyhzgpqlwfkjqipuu"
117 "jvwtxlbznryjdohbvghmyuiggtyqjtmuqinntqmihntkddnalwnmsxsatqqeldacnnpjfe"
118 "rmrnyuqnwbjjpdjhdeavkn"
119 "ykpoxhxclqqedqavdwzoiorrwwxyrhlsrdgqkduvtmzzczufvtvfioygkvedervvudnegh"
120 "bctcbxdxezrzgbpfhzanff"
121 "eccbgqfmzjqtlrsppxqiywjobspefujlxnmddurddiyobqfspvcoulcvdrzkmkwlyiqdch"
122 "ghrgytzdnobqcvdeqjystm"
123 "epxcaniewqmoxkjwpymqorluxedvywhcoghotpusfgiestckrpaigocfufbubiyrrffmwa"
124 "eeimidfnnzcphkflpbqsvt"
125 "dwludsgaungfzoihbxifoprwcjzsdxngtacw";
126
127 char* r04 =
128 "cbxdwjccgtdoqiscyspqzvuqivzptlpvooynyapgvswoaosaghrffnxnjyeeltzaiznicc"
129 "ozwknwyhzgpqlwfkjqipuu"
130 "jvwtxlbznryjdohbvghmyuiggtyqjtmuqinntqmihntkddnalwnmsxsatqqeldacnnpjfe"
131 "rmrnyuqnwbjjpdjhdeavkn"
132 "ykpoxhxclqqedqavdwzoiorrwwxyrhlsrdgqkduvtmzzczufvtvfioygkvedervvudnegh"
133 "bctcbxdxezrzgbpfhzanff"
134 "eccbgqfmzjqtlrsppxqiywjobspefujlxnmddurddiyobqfspvcoulcvdrzkmkwlyiqdch"
135 "ghrgytzdnobqcvdeqjystm"
136 "epxcaniewqmoxkjwpymqorluxedvywhcoghotpusfgiestckrpaigocfufbubiyrrffmwa"
137 "eeimidfnnzwccpahtkgfnl"
138 "xpdbsqzsjvctwdrwploufdisxgbahuinogzf";
139
140 testZigZag(s04, 472, r04);
141}
static void testZigZag(char *s, int numRows, char *expected)
Self-test implementations.
Definition 6.c:86
Here is the call graph for this function:

◆ testZigZag()

static void testZigZag ( char *  s,
int  numRows,
char *  expected 
)
static

Self-test implementations.

Returns
void
87{
88 char* ret = convert(s, numRows);
89 int len = strlen(s);
90 int cmp = strncmp(ret, expected, len);
91 assert(!cmp);
92
93 free(ret);
94}
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
Here is the call graph for this function: