Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
Signed-off-by: begeekmyfriend <[email protected]>
  • Loading branch information
begeekmyfriend committed Aug 18, 2017
1 parent 426be1d commit 743ca6b
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 33 deletions.
121 changes: 112 additions & 9 deletions 037_sudoku_solver/sudoku_solver.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,26 +111,129 @@ static void solveSudoku(char** board, int boardRowSize, int boardColSize)
}
}

int main(int argc, char **argv)
int main(void)
{
int i, j;
char *str = argv[1];
char **board = malloc(9 * sizeof(char *));
board[0] = malloc(10);
board[0][0] = '5';
board[0][1] = '3';
board[0][2] = '.';
board[0][3] = '.';
board[0][4] = '7';
board[0][5] = '.';
board[0][6] = '.';
board[0][7] = '.';
board[0][8] = '.';
board[0][9] = '\0';

board[1] = malloc(10);
board[1][0] = '6';
board[1][1] = '.';
board[1][2] = '.';
board[1][3] = '1';
board[1][4] = '9';
board[1][5] = '5';
board[1][6] = '.';
board[1][7] = '.';
board[1][8] = '.';
board[1][9] = '\0';

board[2] = malloc(10);
board[2][0] = '.';
board[2][1] = '9';
board[2][2] = '8';
board[2][3] = '.';
board[2][4] = '.';
board[2][5] = '.';
board[2][6] = '.';
board[2][7] = '6';
board[2][8] = '.';
board[2][9] = '\0';

board[3] = malloc(10);
board[3][0] = '8';
board[3][1] = '.';
board[3][2] = '.';
board[3][3] = '.';
board[3][4] = '6';
board[3][5] = '.';
board[3][6] = '.';
board[3][7] = '.';
board[3][8] = '3';
board[3][9] = '\0';

board[4] = malloc(10);
board[4][0] = '4';
board[4][1] = '.';
board[4][2] = '.';
board[4][3] = '8';
board[4][4] = '.';
board[4][5] = '3';
board[4][6] = '.';
board[4][7] = '.';
board[4][8] = '1';
board[4][9] = '\0';

board[5] = malloc(10);
board[5][0] = '7';
board[5][1] = '.';
board[5][2] = '.';
board[5][3] = '.';
board[5][4] = '2';
board[5][5] = '.';
board[5][6] = '.';
board[5][7] = '.';
board[5][8] = '6';
board[5][9] = '\0';

board[6] = malloc(10);
board[6][0] = '.';
board[6][1] = '6';
board[6][2] = '.';
board[6][3] = '.';
board[6][4] = '.';
board[6][5] = '.';
board[6][6] = '2';
board[6][7] = '8';
board[6][8] = '.';
board[6][9] = '\0';

board[7] = malloc(10);
board[7][0] = '.';
board[7][1] = '.';
board[7][2] = '.';
board[7][3] = '4';
board[7][4] = '1';
board[7][5] = '9';
board[7][6] = '.';
board[7][7] = '.';
board[7][8] = '5';
board[7][9] = '\0';

board[8] = malloc(10);
board[8][0] = '.';
board[8][1] = '.';
board[8][2] = '.';
board[8][3] = '.';
board[8][4] = '8';
board[8][5] = '.';
board[8][6] = '.';
board[8][7] = '7';
board[8][8] = '9';
board[8][9] = '\0';

for (i = 0; i < 9; i++) {
board[i] = malloc(10);
memcpy(board[i], str + i * 9, 9);
board[9] = '\0';
char *row = board[i];
for (j = 0; j < 9; j++) {
printf("%c ", row[j]);
printf("%c ", board[i][j]);
}
printf("\n");
}
printf("\n");
solveSudoku(board, 9, 9);
for (i = 0; i < 9; i++) {
char *row = board[i];
for (j = 0; j < 9; j++) {
printf("%c ", row[j]);
printf("%c ", board[i][j]);
}
printf("\n");
}
Expand Down
39 changes: 15 additions & 24 deletions 076_minimum_window_substring/window_substring.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,15 @@ static char *minWindow(char *s, char *t)
* m[]: stores the chars in t
*/
int i, f[256], m[256], pat_len = 0;
const int NOT_EXISTED = -1;
const int NOT_FOUND = 0;
memset(m, NOT_EXISTED, sizeof(m));
memset(f, NOT_EXISTED, sizeof(f));
memset(m, 0, sizeof(m));
memset(f, 0, sizeof(f));

/*
* Go through t, and inital the m[] and f[]
* Notes: a same char can be appeared multiple times.
* Notes: duplicate char is allowed.
*/
for (i = 0; t[i] != '\0'; i++) {
m[t[i]] == NOT_EXISTED ? m[t[i]] = 1 : m[t[i]]++;
f[t[i]] = NOT_FOUND;
m[t[i]]++;
pat_len++;
}

Expand All @@ -31,43 +28,37 @@ static char *minWindow(char *s, char *t)
int found = 0;
int begin = 0;
for (i = 0; s[i] != '\0'; i++) {
/* if s[i] is existed in t */
if (m[s[i]] != NOT_EXISTED) {
/* First, find the right side of the window which should be in t */
if (m[s[i]] > 0) {
/* if one char has been found enough times, then do not do found++ */
if (++f[s[i]] <= m[s[i]]) {
found++;
}

if (found >= pat_len) {
/*
* Find the beginning of the window
* 1) f[s[begin]] == NOT_EXISTED ===> the char at the `begin` is not in t
* 2) f[s[begin]] > m[s[begin]] ===> a same char appeared more than excepted.
*/
while (f[s[begin]] == NOT_EXISTED || f[s[begin]] > m[s[begin]]) {
/* the right side of the window is confirmed as i */
/* The found counter will no more increase if the first right side of the window is confirmed,
* the next step run here can also be regarded as a new right side of a new window. */
if (found == pat_len) {
/* Then we need to find the left side of the window
* 1) m[s[begin]] == 0 => Both left and right side should be found in t
* 2) f[s[begin]] > m[s[begin]] => duplicate chars are more than excepted in the window so that we can even shrink the size. */
while (m[s[begin]] == 0 || f[s[begin]] > m[s[begin]]) {
if (f[s[begin]] > m[s[begin]]) {
f[s[begin]]--;
}
begin++;
}

int j;
printf("%d %d\n", begin, i);
for (j = begin; j < i - begin + 1; j++) {
printf("%c", s[j]);
}
printf("\n");
/* Calculate the minimized window size */
if (size > i - begin + 1) {
start = begin;
size = i - begin + 1;
printf("size:%d\n", size);
}
}
}
}

char *result = NULL;
char *result;
if (start >= 0 && size > 0) {
result = malloc(size + 1);
memcpy(result, s + start, size);
Expand Down

0 comments on commit 743ca6b

Please sign in to comment.