Skip to content

Commit 3c3d486

Browse files
Fix
Signed-off-by: begeekmyfriend <begeekmyfriend@gmail.com>
1 parent 86322be commit 3c3d486

File tree

3 files changed

+126
-209
lines changed

3 files changed

+126
-209
lines changed

034_search_for_a_range/range_search.c

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,43 @@
11
#include <stdio.h>
22
#include <stdlib.h>
33

4-
static int binary_search_start(int *a, int size, int target)
4+
static int binary_search_begin(int *a, int size, int target)
55
{
6-
int low = -1;
7-
int high = size;
8-
while (low + 1 < high) {
9-
int mid = low + (high - low) / 2;
6+
int lo = -1;
7+
int hi = size;
8+
while (lo + 1 < hi) {
9+
int mid = lo + (hi - lo) / 2;
1010
if (target > a[mid]) {
11-
low = mid;
11+
lo = mid;
1212
} else {
13-
high = mid;
13+
hi = mid;
1414
}
1515
}
1616

17-
if (high == size || a[high] != target) {
17+
if (hi == size || a[hi] != target) {
1818
return -1;
1919
} else {
20-
return high;
20+
return hi;
2121
}
2222
}
2323

2424
static int binary_search_end(int *a, int size, int target)
2525
{
26-
int low = -1;
27-
int high = size;
28-
while (low + 1 < high) {
29-
int mid = low + (high - low) / 2;
26+
int lo = -1;
27+
int hi = size;
28+
while (lo + 1 < hi) {
29+
int mid = lo + (hi - lo) / 2;
3030
if (target < a[mid]) {
31-
high = mid;
31+
hi = mid;
3232
} else {
33-
low = mid;
33+
lo = mid;
3434
}
3535
}
3636

37-
if (low == -1 || a[low] != target) {
37+
if (lo == -1 || a[lo] != target) {
3838
return -1;
3939
} else {
40-
return low;
40+
return lo;
4141
}
4242
}
4343

@@ -49,13 +49,7 @@ static int* searchRange(int* nums, int numsSize, int target, int* returnSize)
4949
{
5050
int *range = malloc(2 * sizeof(int));
5151
*returnSize = 2;
52-
53-
if (numsSize == 0) {
54-
range[0] = range[1] = -1;
55-
return range;
56-
}
57-
58-
range[0] = binary_search_start(nums, numsSize, target);
52+
range[0] = binary_search_begin(nums, numsSize, target);
5953
range[1] = binary_search_end(nums, numsSize, target);
6054
return range;
6155
}

036_valid_sudoku/valid_sudoku.c

Lines changed: 37 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -3,138 +3,83 @@
33
#include <stdbool.h>
44
#include <string.h>
55

6-
struct stack {
7-
int row;
8-
int col;
9-
int value;
10-
};
116

12-
static bool valid(char **board, int row, int col)
7+
static bool valid(char **board, char *mark, int i, int j) {
8+
if (board[i][j] != '.') {
9+
int index = board[i][j] - '0';
10+
if (mark[index]) {
11+
return false;
12+
} else {
13+
mark[index] = 1;
14+
}
15+
}
16+
return true;
17+
}
18+
19+
bool isValidSudoku(char** board, int boardSize, int* boardColSize)
1320
{
1421
int i, j, k;
15-
char mark[10];
1622

17-
for (i = 0; i <= row; i++) {
18-
memset(mark, 0, 10);
19-
/* check row validity */
20-
for (j = 0; j < 9; j++) {
21-
if (board[i][j] != '.') {
22-
int index = board[i][j] - '0';
23-
if (mark[index]) {
24-
return false;
25-
} else {
26-
mark[index] = 1;
27-
}
23+
/* check row validity */
24+
for (i = 0; i < boardSize; i++) {
25+
char mark[10] = { '\0' };
26+
for (j = 0; j < boardColSize[i]; j++) {
27+
if (!valid(board, mark, i, j)) {
28+
return false;
2829
}
2930
}
3031
}
3132

3233
/* check column validity */
33-
for (i = 0; i <= col; i++) {
34-
memset(mark, 0, 10);
35-
for (j = 0; j < 9; j++) {
36-
if (board[j][i] != '.') {
37-
int index = board[j][i] - '0';
38-
if (mark[index]) {
39-
return false;
40-
} else {
41-
mark[index] = 1;
42-
}
34+
for (j = 0; j < boardColSize[0]; j++) {
35+
char mark[10] = { '\0' };
36+
for (i = 0; i < boardSize; i++) {
37+
if (!valid(board, mark, i, j)) {
38+
return false;
4339
}
4440
}
4541
}
4642

4743
/* check sub-box validity */
48-
int count = row / 3 * 3 + col / 3 + 1;
49-
for (k = 0; k < count; k++) {
44+
for (k = 0; k < boardSize; k++) {
5045
int sr = k / 3 * 3;
5146
int sc = (k % 3) * 3;
52-
memset(mark, 0, 10);
47+
char mark[10] = { '\0' };
5348
for (i = sr; i < sr + 3; i++) {
5449
for (j = sc; j < sc + 3; j++) {
55-
if (board[i][j] != '.') {
56-
int index = board[i][j] - '0';
57-
if (mark[index]) {
58-
return false;
59-
} else {
60-
mark[index] = 1;
61-
}
62-
}
63-
}
64-
}
65-
}
66-
67-
return true;
68-
}
69-
70-
static bool isValidSudoku(char** board, int boardRowSize, int boardColSize)
71-
{
72-
if (boardRowSize != 9 || boardColSize != 9) {
73-
return false;
74-
}
75-
76-
int i = 0, j = 0, k = 1, num = 0;
77-
struct stack stack[81];
78-
79-
while (i < boardRowSize) {
80-
if (board[i][j] == '.') {
81-
while (k <= 9) {
82-
board[i][j] = k + '0';
83-
if (valid(board, i, j)) {
84-
stack[num].row = i;
85-
stack[num].col = j;
86-
stack[num].value = k;
87-
num++;
88-
k = 1;
89-
break;
90-
}
91-
k++;
92-
}
93-
if (k == 10) {
94-
if (num == 0) {
50+
if (!valid(board, mark, i, j)) {
9551
return false;
9652
}
97-
board[i][j] = '.';
98-
--num;
99-
i = stack[num].row;
100-
j = stack[num].col;
101-
k = stack[num].value + 1;
102-
board[i][j] = '.';
103-
continue;
10453
}
10554
}
106-
/* next row */
107-
if (++j == boardColSize) {
108-
j = 0;
109-
i++;
110-
}
11155
}
112-
56+
11357
return true;
11458
}
11559

11660
int main(int argc, char **argv)
11761
{
11862
int i, j;
11963
char *str = argv[1];
64+
int *col_sizes = malloc(9 * sizeof(int));
12065
char **board = malloc(9 * sizeof(char *));
12166
for (i = 0; i < 9; i++) {
12267
board[i] = malloc(10);
123-
memcpy(board[i], str + i * 9, 9);
124-
board[9] = '\0';
125-
char *row = board[i];
68+
strcpy(board[i], str + i * 9);
12669
for (j = 0; j < 9; j++) {
127-
printf("%c ", row[j]);
70+
printf("%c ", board[i][j]);
12871
}
72+
col_sizes[i] = 9;
12973
printf("\n");
13074
}
131-
printf("%s\n", isValidSudoku(board, 9, 9) ? "true" : "false");
75+
76+
printf("%s\n", isValidSudoku(board, 9, col_sizes) ? "true" : "false");
13277
for (i = 0; i < 9; i++) {
133-
char *row = board[i];
13478
for (j = 0; j < 9; j++) {
135-
printf("%c ", row[j]);
79+
printf("%c ", board[i][j]);
13680
}
13781
printf("\n");
13882
}
139-
return;
83+
84+
return 0;
14085
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy