165. 比较版本号
/*
0~9 .
char * strtok ( char * str, const char * delimiters );
这题 感觉就是 这个函数的应用
*/
#define BUFLEN 10000
#define MIN(x,y) (x < y ? x : y)
typedef struct {
int arr[BUFLEN];
int len;
} Arr;
void initArr(Arr * a)
{
memset(a->arr, 0, sizeof(int) * BUFLEN);
a->len = -1;
}
void trans(char * str, Arr *v)
{
char *token = NULL;
token = strtok(str, ".");
while (token != NULL) {
v->arr[++(v->len)] = atoi(token);
token = strtok(NULL, ".");
}
}
int compareVersion(char * version1, char * version2){
Arr v1, v2;
initArr(&v1);
initArr(&v2);
trans(version1, &v1);
trans(version2, &v2);
// for (int i = 0; i <= v1.len; i++) {
// printf("%d ", v1.arr[i]);
// }
for (int i = 0; i <= MIN(v1.len, v2.len); i++) {
if (v1.arr[i] == v2.arr[i]) {
continue;
}
if (v1.arr[i] < v2.arr[i]) {
return -1;
} else {
return 1;
}
}
if (v1.len == v2.len) {
return 0;
}
int sum = 0;
if (v1.len > v2.len) {
for (int i = v2.len + 1; i <=v1.len; i++) {
sum += v1.arr[i];
}
return (sum > 0 ? 1 : 0);
} else {
for (int j = v1.len + 1; j <= v2.len; j++) {
sum += v2.arr[j];
}
return (sum > 0 ? -1 : 0);
}
return 1;
}
130. 被围绕的区域
/*
DFS 找边界
遇到个问题
1.
// int colour[BUFSIZE][BUFSIZE] = {0}; error
2. 必须要用分配内存
int **colour = (int **)malloc(sizeof(int *) * boardSize);
for (int i = 0; i < boardSize; i++) {
colour[i] = (int *)malloc(sizeof(int) * (*boardColSize));
memset(colour[i], 0, sizeof(int) * (*boardColSize));
}
两种 数组 静态的
*/
// #define BUFSIZE 100
void dfs(char** board, int boardSize, int* boardColSize, int *res, int i, int j, int **colour, int t)
{
//printf("%d\n", colour[i][j]); // ??
if (colour[i][j] == t || board[i][j] == 'X') {
return;
}
colour[i][j] = t;
if ((i == 0 || j == 0 || i == boardSize - 1 || j == *boardColSize -1) && (board[i][j] == 'O')) {
*res = -1;
}
if (i > 0) {
dfs(board, boardSize, boardColSize, res, i - 1, j, colour, t);
}
if (i < boardSize - 1) {
dfs(board, boardSize, boardColSize, res, i + 1, j, colour, t);
}
if (j > 0) {
dfs(board, boardSize, boardColSize, res, i, j - 1, colour, t);
}
if (j < *boardColSize -1) {
dfs(board, boardSize, boardColSize, res, i, j + 1, colour, t);
}
}
void change(char** board, int boardSize, int* boardColSize, int **colour, int t)
{
for (int i = 0; i < boardSize; i++) {
for (int j = 0; j < *boardColSize; j++) {
if (colour[i][j] == t) {
board[i][j] = 'X';
printf("[%d,%d]", i,j);
}
}
}
}
void solve(char** board, int boardSize, int* boardColSize){
// int colour[BUFSIZE][BUFSIZE] = {0};// AddressSanitizer
int **colour = (int **)malloc(sizeof(int *) * boardSize);
for (int i = 0; i < boardSize; i++) {
colour[i] = (int *)malloc(sizeof(int) * (*boardColSize));
memset(colour[i], 0, sizeof(int) * (*boardColSize));
}
int res;
int t = 0;
for (int i = 0; i < boardSize; i++) {
for (int j = 0; j < *boardColSize; j++) {
// printf("idx %d %d\n", i,j);
if (board[i][j] == 'O'&& colour[i][j] == 0) {
res = 0;
t++;
dfs(board, boardSize, boardColSize, &res, i, j, colour, t);
if (res == 0) {
change(board, boardSize, boardColSize, colour, t);
}
}
}
}
for (int i = 0; i < boardSize; i++) {
free(colour[i]);
}
free(colour);
}
网友评论