-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
Copy path6.c
151 lines (132 loc) · 3.83 KB
/
6.c
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**
* @file
* @brief Implementation of the [ZigZag
* Conversion](https://leetcode.com/problems/zigzag-conversion/) Leetcode
* problem
* @details
* 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](https://github.com/straight-into-the-wall)
*/
#include <assert.h> /// for assert
#include <stdint.h> /// for unsigned int with fixed size
#include <stdio.h> /// for IO operations
#include <stdlib.h> /// for malloc
#include <string.h> /// for string tools
/**
* @brief Convert a string to the it's zigzag equivalent on a given number of
* rows.
* @param in the string in input.
* @param numRows the desired number of rows.
* @returns the converted new (malloced) string.
*/
char* convert(char* in, uint16_t numRows)
{
uint16_t len = strlen(in);
if (len < numRows)
{
numRows = len;
}
char* out = calloc(len + 1, sizeof(char));
if (numRows < 2)
{
memcpy(out, in, len + 1);
return out;
}
uint16_t max = numRows - 1;
uint16_t rr = 2 * max;
uint16_t i = 0;
uint16_t o = 0;
uint16_t delta = 0;
// first row
while (i < len)
{
out[o++] = in[i];
i += rr;
}
// middle rows
for (uint16_t l = 1; l < max; l++)
{
i = l;
delta = 2 * l;
while (i < len)
{
out[o++] = in[i];
delta = rr - delta;
i += delta;
}
}
// last row
i = max;
while (i < len)
{
out[o++] = in[i];
i += rr;
}
return out;
}
/**
* @brief Self-test implementations
* @returns void
*/
static void testZigZag(char* s, int numRows, char* expected)
{
char* ret = convert(s, numRows);
int len = strlen(s);
int cmp = strncmp(ret, expected, len);
assert(!cmp);
free(ret);
}
/**
* @brief Self-test implementations
* @returns void
*/
static void test()
{
char* s01 = "PAYPALISHIRING";
char* r01 = "PINALSIGYAHRPI";
testZigZag(s01, 4, r01);
char* r02 = "PAHNAPLSIIGYIR";
testZigZag(s01, 3, r02);
char* s03 = "A";
testZigZag(s03, 1, s03);
testZigZag(s03, 3, s03);
char* s04 =
"cbxdwjccgtdoqiscyspqzvuqivzptlpvooynyapgvswoaosaghrffnxnjyeeltzaiznicc"
"ozwknwyhzgpqlwfkjqipuu"
"jvwtxlbznryjdohbvghmyuiggtyqjtmuqinntqmihntkddnalwnmsxsatqqeldacnnpjfe"
"rmrnyuqnwbjjpdjhdeavkn"
"ykpoxhxclqqedqavdwzoiorrwwxyrhlsrdgqkduvtmzzczufvtvfioygkvedervvudnegh"
"bctcbxdxezrzgbpfhzanff"
"eccbgqfmzjqtlrsppxqiywjobspefujlxnmddurddiyobqfspvcoulcvdrzkmkwlyiqdch"
"ghrgytzdnobqcvdeqjystm"
"epxcaniewqmoxkjwpymqorluxedvywhcoghotpusfgiestckrpaigocfufbubiyrrffmwa"
"eeimidfnnzcphkflpbqsvt"
"dwludsgaungfzoihbxifoprwcjzsdxngtacw";
char* r04 =
"cbxdwjccgtdoqiscyspqzvuqivzptlpvooynyapgvswoaosaghrffnxnjyeeltzaiznicc"
"ozwknwyhzgpqlwfkjqipuu"
"jvwtxlbznryjdohbvghmyuiggtyqjtmuqinntqmihntkddnalwnmsxsatqqeldacnnpjfe"
"rmrnyuqnwbjjpdjhdeavkn"
"ykpoxhxclqqedqavdwzoiorrwwxyrhlsrdgqkduvtmzzczufvtvfioygkvedervvudnegh"
"bctcbxdxezrzgbpfhzanff"
"eccbgqfmzjqtlrsppxqiywjobspefujlxnmddurddiyobqfspvcoulcvdrzkmkwlyiqdch"
"ghrgytzdnobqcvdeqjystm"
"epxcaniewqmoxkjwpymqorluxedvywhcoghotpusfgiestckrpaigocfufbubiyrrffmwa"
"eeimidfnnzwccpahtkgfnl"
"xpdbsqzsjvctwdrwploufdisxgbahuinogzf";
testZigZag(s04, 472, r04);
}
/**
* @brief Main function
* @returns 0 on exit
*/
int main(void)
{
test(); // run self-test implementations
return 0;
}