Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit aca3d04

Browse files
Improvement
Signed-off-by: begeekmyfriend <begeekmyfriend@gmail.com>
1 parent c8116b5 commit aca3d04

File tree

3 files changed

+58
-41
lines changed

3 files changed

+58
-41
lines changed

‎0128_longest_consecutive_sequence/consec_seq.c‎

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -94,26 +94,15 @@ int longestConsecutive(int* nums, int numsSize)
9494
}
9595

9696
for (i = 0; i < numsSize; i++) {
97-
int len = 0;
98-
int num;
99-
node = find(nums[i], numsSize, heads);
100-
while (node != NULL) {
101-
len++;
102-
num = node->num;
103-
list_del(&node->link);
104-
105-
int left = num;
106-
while ((node = find(--left, numsSize, heads)) != NULL) {
97+
/* Find the first consecutive number */
98+
node = find(nums[i] - 1, numsSize, heads);
99+
if (node == NULL) {
100+
int len = 0;
101+
int num = nums[i];
102+
while ((node = find(num++, numsSize, heads)) != NULL) {
107103
len++;
108104
list_del(&node->link);
109105
}
110-
111-
int right = num;
112-
while ((node = find(++right, numsSize, heads)) != NULL) {
113-
len++;
114-
list_del(&node->link);
115-
}
116-
117106
length = len > length ? len : length;
118107
}
119108
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
class Solution {
6+
public:
7+
int longestConsecutive(vector<int>& nums) {
8+
int res = 0;
9+
unordered_set<int> s;
10+
for (int i = 0; i < nums.size(); i++) {
11+
s.insert(nums[i]);
12+
}
13+
for (int n : nums) {
14+
if (!s.count(n - 1)) {
15+
int len = 0;
16+
int num = n;
17+
while (s.count(num)) {
18+
s.erase(num);
19+
num++;
20+
len++;
21+
}
22+
res = len > res ? len : res;
23+
}
24+
}
25+
return res;
26+
}
27+
};

‎0130_surrounded_regions/surrounded_regions.c‎

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ static void bfs(char **board, int row_size, int col_size,
117117
}
118118
}
119119

120-
void solve(char** board, int boardRowSize, int boardColSize)
120+
void solve(char** board, int boardSize, int *boardColSize)
121121
{
122122
int i, j;
123123
struct node *new;
@@ -126,48 +126,48 @@ void solve(char** board, int boardRowSize, int boardColSize)
126126
INIT_LIST_HEAD(&queue);
127127
INIT_LIST_HEAD(&free_list);
128128

129-
for (i = 0; i < boardColSize; i++) {
129+
for (i = 0; i < boardColSize[0]; i++) {
130130
if (board[0][i] == 'O') {
131131
new = node_new(&free_list);
132132
new->x = 0;
133133
new->y = i;
134134
list_add_tail(&new->link, &queue);
135-
bfs(board, boardRowSize, boardColSize, &queue, &free_list);
135+
bfs(board, boardSize, boardColSize[0], &queue, &free_list);
136136
}
137137
}
138138

139-
for (i = 0; i < boardColSize; i++) {
140-
if (board[boardRowSize - 1][i] == 'O') {
139+
for (i = 0; i < boardColSize[0]; i++) {
140+
if (board[boardSize - 1][i] == 'O') {
141141
new = node_new(&free_list);
142-
new->x = boardRowSize - 1;
142+
new->x = boardSize - 1;
143143
new->y = i;
144144
list_add_tail(&new->link, &queue);
145-
bfs(board, boardRowSize, boardColSize, &queue, &free_list);
145+
bfs(board, boardSize, boardColSize[0], &queue, &free_list);
146146
}
147147
}
148148

149-
for (i = 0; i < boardRowSize; i++) {
149+
for (i = 0; i < boardSize; i++) {
150150
if (board[i][0] == 'O') {
151151
new = node_new(&free_list);
152152
new->x = i;
153153
new->y = 0;
154154
list_add_tail(&new->link, &queue);
155-
bfs(board, boardRowSize, boardColSize, &queue, &free_list);
155+
bfs(board, boardSize, boardColSize[i], &queue, &free_list);
156156
}
157157
}
158158

159-
for (i = 0; i < boardRowSize; i++) {
160-
if (board[i][boardColSize - 1] == 'O') {
159+
for (i = 0; i < boardSize; i++) {
160+
if (board[i][boardColSize[i] - 1] == 'O') {
161161
new = node_new(&free_list);
162162
new->x = i;
163-
new->y = boardColSize - 1;
163+
new->y = boardColSize[i] - 1;
164164
list_add_tail(&new->link, &queue);
165-
bfs(board, boardRowSize, boardColSize, &queue, &free_list);
165+
bfs(board, boardSize, boardColSize[i], &queue, &free_list);
166166
}
167167
}
168168

169-
for (i = 0; i < boardRowSize; i++) {
170-
for (j = 0; j < boardColSize; j++) {
169+
for (i = 0; i < boardSize; i++) {
170+
for (j = 0; j < boardColSize[i]; j++) {
171171
board[i][j] = board[i][j] == 'P' ? 'O' : 'X';
172172
}
173173
}
@@ -177,50 +177,51 @@ int main(void)
177177
{
178178
int i, j;
179179
int row_size = 5;
180-
int col_size = 5;
180+
int *col_sizes = malloc(row_size*sizeof(int));
181181
char **board = malloc(row_size * sizeof(char *));
182-
board[0] = malloc(col_size);
182+
board[0] = malloc(row_size);
183183
board[0][0] = 'X';
184184
board[0][1] = 'X';
185185
board[0][2] = 'X';
186186
board[0][3] = 'X';
187187
board[0][4] = 'X';
188-
board[1] = malloc(col_size);
188+
board[1] = malloc(row_size);
189189
board[1][0] = 'O';
190190
board[1][1] = 'X';
191191
board[1][2] = 'O';
192192
board[1][3] = 'O';
193193
board[1][4] = 'X';
194-
board[2] = malloc(col_size);
194+
board[2] = malloc(row_size);
195195
board[2][0] = 'O';
196196
board[2][1] = 'O';
197197
board[2][2] = 'X';
198198
board[2][3] = 'O';
199199
board[2][4] = 'X';
200-
board[3] = malloc(col_size);
200+
board[3] = malloc(row_size);
201201
board[3][0] = 'X';
202202
board[3][1] = 'X';
203203
board[3][2] = 'O';
204204
board[3][3] = 'X';
205205
board[3][4] = 'X';
206-
board[4] = malloc(col_size);
206+
board[4] = malloc(row_size);
207207
board[4][0] = 'X';
208208
board[4][1] = 'X';
209209
board[4][2] = 'O';
210210
board[4][3] = 'O';
211211
board[4][4] = 'X';
212212

213213
for (i = 0; i < row_size; i++) {
214-
for (j = 0; j < col_size; j++) {
214+
col_sizes[i] = row_size;
215+
for (j = 0; j < col_sizes[i]; j++) {
215216
printf("%c ", board[i][j]);
216217
}
217218
printf("\n");
218219
}
219220
printf("\n");
220221

221-
solve(board, row_size, col_size);
222+
solve(board, row_size, col_sizes);
222223
for (i = 0; i < row_size; i++) {
223-
for (j = 0; j < col_size; j++) {
224+
for (j = 0; j < col_sizes[i]; j++) {
224225
printf("%c ", board[i][j]);
225226
}
226227
printf("\n");

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /