Skip to content

Commit c48f7fe

Browse files
author
vli02
committed
removed non-ascii characters.
1 parent 98054c7 commit c48f7fe

File tree

3 files changed

+92
-92
lines changed

3 files changed

+92
-92
lines changed

131. Palindrome Partitioning.c

Lines changed: 58 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -22,74 +22,74 @@ Return
2222
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
2323
*/
2424
typedef struct {
25-
   int start;
26-
   int len;
25+
int start;
26+
int len;
2727
} buff_t;
2828
typedef struct {
29-
   char ***p;
30-
   int *csz;
31-
   int psz;
32-
   int pn;
29+
char ***p;
30+
int *csz;
31+
int psz;
32+
int pn;
3333
} res_t;
3434
void add2res(res_t *res, const char *s, buff_t *buff, int d) {
35-
   int i;
36-
   char *str, **pp;
37-
   if (res->psz == res->pn) {
38-
       res->psz = (res->psz == 0) ? 10 : res->psz * 2;
39-
       res->p = realloc(res->p, res->psz * sizeof(char **));
40-
       res->csz = realloc(res->csz, res->psz * sizeof(int));
41-
       //assert(res->p && res->psz);
42-
  }
43-
   pp = malloc(d * sizeof(char *));
44-
   //assert(pp);
45-
   for (i = 0; i < d; i ++) {
46-
       str = strndup(&s[buff[i].start], buff[i].len);
47-
       pp[i] = str;
48-
  }
49-
   res->p[res->pn] = pp;
50-
   res->csz[res->pn ++] = d;
35+
int i;
36+
char *str, **pp;
37+
if (res->psz == res->pn) {
38+
res->psz = (res->psz == 0) ? 10 : res->psz * 2;
39+
res->p = realloc(res->p, res->psz * sizeof(char **));
40+
res->csz = realloc(res->csz, res->psz * sizeof(int));
41+
//assert(res->p && res->psz);
42+
}
43+
pp = malloc(d * sizeof(char *));
44+
//assert(pp);
45+
for (i = 0; i < d; i ++) {
46+
str = strndup(&s[buff[i].start], buff[i].len);
47+
pp[i] = str;
48+
}
49+
res->p[res->pn] = pp;
50+
res->csz[res->pn ++] = d;
5151
}
5252
int is_palindrome(const char *s, int start, int end) {
53-
   while (start < end) {
54-
       if (s[start] != s[end]) return 0;
55-
       start ++;
56-
       end --;
57-
  }
58-
   return 1;
53+
while (start < end) {
54+
if (s[start] != s[end]) return 0;
55+
start ++;
56+
end --;
57+
}
58+
return 1;
5959
}
6060
void bt(const char *s, int start, int sz, buff_t *buff, int d, res_t *res) {
61-
   int i;
62-
   if (start == sz) {
63-
       // done, save result
64-
       add2res(res, s, buff, d);
65-
       return;
66-
  }
67-
   for (i = start; i < sz; i ++) {
68-
       if (is_palindrome(s, start, i)) {
69-
           buff[d].start = start;
70-
           buff[d].len = i - start + 1;
71-
           bt(s, i + 1, sz, buff, d + 1, res);
72-
      }
73-
  }
61+
int i;
62+
if (start == sz) {
63+
// done, save result
64+
add2res(res, s, buff, d);
65+
return;
66+
}
67+
for (i = start; i < sz; i ++) {
68+
if (is_palindrome(s, start, i)) {
69+
buff[d].start = start;
70+
buff[d].len = i - start + 1;
71+
bt(s, i + 1, sz, buff, d + 1, res);
72+
}
73+
}
7474
}
7575
char*** partition(char* s, int** columnSizes, int* returnSize) {
76-
   res_t res = { 0 };
77-
   buff_t *buff;
78-
   int sz;
79-
   
80-
   sz = strlen(s);
81-
   
82-
   buff = malloc(sz * sizeof(buff_t));
83-
   //assert(buff);
84-
   
85-
   bt(s, 0, sz, buff, 0, &res);
86-
   
87-
   //free(buff);
88-
   
89-
   *columnSizes = res.csz;
90-
   *returnSize = res.pn;
91-
   
92-
   return res.p;
76+
res_t res = { 0 };
77+
buff_t *buff;
78+
int sz;
79+
80+
sz = strlen(s);
81+
82+
buff = malloc(sz * sizeof(buff_t));
83+
//assert(buff);
84+
85+
bt(s, 0, sz, buff, 0, &res);
86+
87+
//free(buff);
88+
89+
*columnSizes = res.csz;
90+
*returnSize = res.pn;
91+
92+
return res.p;
9393
}
9494

9595

214. Shortest Palindrome.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ Credits:Special thanks to @ifanchu for adding this problem and creating all test
1212
*/
1313

1414
char* shortestPalindrome(char* s) {
15-
   // two pointers, head and tail
16-
   // verify if it is a valid palindrome
17-
   // if not, reduce tail pointer and repeat verify
18-
   // if yes, stop verify
19-
   // add characters from end to the tail pointer to a buffer
20-
   // append characters from head to tail to the buffer
21-
   // done
22-
   return s;
15+
// two pointers, head and tail
16+
// verify if it is a valid palindrome
17+
// if not, reduce tail pointer and repeat verify
18+
// if yes, stop verify
19+
// add characters from end to the tail pointer to a buffer
20+
// append characters from head to tail to the buffer
21+
// done
22+
return s;
2323
}
2424

2525

5. Longest Palindromic Substring.c

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,32 @@ Output: "bb"
1919
*/
2020

2121
char* longestPalindrome(char* s) {
22-
   int sz;
23-
   int i, j, k, l;
24-
   int where = 0, len = 0;
25-
   
26-
   if (!s || !*s) return s;
27-
   
28-
   sz = strlen(s);
29-
30-
   for (i = 0, j = 0, k = 0; len < (sz - k) * 2; i = k, j = k) {
31-
       while (j + 1 < sz && s[j] == s[j + 1]) {
32-
           j ++;  // skip all repeating characters
33-
      }
34-
       k = j + 1; // where to start next try
35-
       while (i > 0 && j + 1 < sz && s[i - 1] == s[j + 1]) {
36-
           i --;  // expand dual direction
37-
           j ++;
38-
      }
39-
       l = j - i + 1; // what we have fond so far
40-
       if (len < l) {
41-
           len = l;
42-
           where = i;
43-
      }
44-
  }
45-
   s = s + where;
46-
   s[len] = 0;
47-
   return s;
22+
int sz;
23+
int i, j, k, l;
24+
int where = 0, len = 0;
25+
26+
if (!s || !*s) return s;
27+
28+
sz = strlen(s);
29+
30+
for (i = 0, j = 0, k = 0; len < (sz - k) * 2; i = k, j = k) {
31+
while (j + 1 < sz && s[j] == s[j + 1]) {
32+
j ++; // skip all repeating characters
33+
}
34+
k = j + 1; // where to start next try
35+
while (i > 0 && j + 1 < sz && s[i - 1] == s[j + 1]) {
36+
i --; // expand dual direction
37+
j ++;
38+
}
39+
l = j - i + 1; // what we have fond so far
40+
if (len < l) {
41+
len = l;
42+
where = i;
43+
}
44+
}
45+
s = s + where;
46+
s[len] = 0;
47+
return s;
4848
}
4949

5050

0 commit comments

Comments
 (0)