Skip to content

Commit 38d13aa

Browse files
committed
update
1 parent 045a4e2 commit 38d13aa

File tree

4 files changed

+39
-49
lines changed

4 files changed

+39
-49
lines changed

CATEGORY.md

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,9 @@ https://garciparedes.me/#headers
9999
- [x] 42
100100
- [ ] 45
101101
- [ ] 55
102-
- [ ] 56
103-
- [ ] 59
104-
- [ ] 62
105-
- [ ] 64
106-
- [ ] 66
107-
- [ ] 73
108-
- [ ] 75
102+
- [x] 56
103+
- [x] 66
104+
- [x] 73
109105
- [ ] 78
110106
- [ ] 80
111107
- [ ] 88

src/56-merge-intervals/index.js

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
1-
export const merge = intervals => {
2-
let res = [];
3-
intervals.sort((x, y) => x.start - y.start);
4-
let current = intervals.slice(0, 1)[0];
1+
const merge = (intervals) => {
2+
intervals.sort((x, y) => x[0] - y[0]);
3+
const res = [];
4+
let temp = intervals[0];
55

6-
for (let { start, end } of intervals.slice(1)) {
7-
if (current.end < start) {
8-
res.push([current.start, current.end]);
9-
current = { start, end };
6+
for (let i = 1; i < intervals.length; i++) {
7+
const cur = intervals[i];
8+
9+
if (cur[0] > temp[1]) {
10+
res.push(temp);
11+
temp = cur;
1012
} else {
11-
current = {
12-
start: Math.min(current.start, start),
13-
end: Math.max(current.end, end),
14-
};
13+
temp[1] = Math.max(cur[1], temp[1]);
1514
}
1615
}
1716

18-
if (current.start !== undefined) {
19-
res.push([current.start, current.end]);
20-
}
17+
res.push(temp);
2118

2219
return res;
2320
};

src/66-plus-one/index.js

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
export function plusOne(digits) {
2-
const lastIndex = digits.length - 1;
3-
let i = lastIndex;
4-
5-
while (i >= 0) {
6-
if (digits[i] !== 9) {
7-
digits[i] += 1;
8-
return digits;
9-
}
2+
let carry = 1;
3+
const res = [];
4+
for (let i = digits.length - 1; i >= 0; i--) {
5+
res.unshift((digits[i] + carry) % 10);
6+
carry = digits[i] + carry === 10 ? 1 : 0;
7+
}
108

11-
digits[i] = 0;
12-
if (i === 0) {
13-
digits.unshift(1);
14-
return digits;
15-
}
16-
i--;
9+
if (carry > 0) {
10+
return [1, ...res];
1711
}
1812

19-
return digits;
13+
return res;
2014
}

src/73-set-matrix-zeroes/index.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
1-
export const setZeroes = matrix => {
2-
const helper = Array.from({ length: matrix.length }, _ =>
3-
Array(matrix[0].length).fill(1),
4-
);
1+
const setZeroes = (matrix) => {
2+
const zeroesI = [];
3+
const zeroesJ = [];
54

65
for (let i = 0; i < matrix.length; i++) {
76
for (let j = 0; j < matrix[0].length; j++) {
8-
if (matrix[i][j] === 0) helper[i][j] = 0;
7+
if (matrix[i][j] === 0) {
8+
zeroesI.push(i);
9+
zeroesJ.push(j);
10+
}
911
}
1012
}
1113

12-
for (let i = 0; i < matrix.length; i++) {
14+
for (let n of zeroesI) {
1315
for (let j = 0; j < matrix[0].length; j++) {
14-
if (helper[i][j] === 0) {
15-
matrix[i].fill(0);
16-
for (let n = 0; n < matrix.length; n++) {
17-
matrix[n][j] = 0;
18-
}
19-
}
16+
matrix[n][j] = 0;
17+
}
18+
}
19+
20+
for (let n of zeroesJ) {
21+
for (let j = 0; j < matrix.length; j++) {
22+
matrix[j][n] = 0;
2023
}
2124
}
2225

0 commit comments

Comments
 (0)