Skip to content

Commit f94caa6

Browse files
author
David Lu
authored
Merge pull request #52 from yungshenglu/develop
Merge develop into master
2 parents cff4124 + bbb546e commit f94caa6

File tree

6 files changed

+112
-8
lines changed

6 files changed

+112
-8
lines changed

LC-412/LC-412.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@ class Solution {
1212
vector<string> fizzBuzz(int n) {
1313
vector<string> result;
1414
for (int i = 1; i <= n; ++i) {
15-
if (i % 15 == 0)
16-
result.push_back("FizzBuzz");
17-
else if (i % 5 == 0 && i % 3 != 0)
18-
result.push_back("Buzz");
19-
else if (i % 3 == 0 && i % 5 != 0)
20-
result.push_back("Fizz");
21-
else
22-
result.push_back(to_string(i));
15+
cout << i << endl;
16+
string temp = "";
17+
if (i % 3 == 0) {
18+
temp += "Fizz";
19+
}
20+
if (i % 5 == 0) {
21+
temp += "Buzz";
22+
}
23+
result.push_back(temp != "" ? temp : to_string(i));
2324
}
2425
return result;
2526
}

LC-412/LC-412.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {number} n
3+
* @return {string[]}
4+
*/
5+
var fizzBuzz = function(n) {
6+
/**
7+
* Concepts:
8+
* Optimize naive approach
9+
*/
10+
11+
let result = [];
12+
for (let i = 1; i <= n; ++i) {
13+
let temp = '';
14+
if (i % 3 === 0) {
15+
temp += 'Fizz';
16+
}
17+
if (i % 5 === 0) {
18+
temp += 'Buzz';
19+
}
20+
result.push(temp ? temp : String(i));
21+
}
22+
return result;
23+
};

LC-73/LC-73-1.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @param {number[][]} matrix
3+
* @return {void} Do not return anything, modify matrix in-place instead.
4+
*/
5+
var setZeroes = function(matrix) {
6+
/**
7+
* Concepts:
8+
* 1. Get the index of row and column that should be zero in matrix
9+
* 2. Set the element that should be zero
10+
*/
11+
12+
let zeroRowIndex = new Set();
13+
let zeroColIndex = new Set();
14+
15+
for (let i = 0; i < matrix.length; ++i) {
16+
for (let j = 0; j < matrix[i].length; ++j) {
17+
if (matrix[i][j] === 0) {
18+
zeroRowIndex.add(i);
19+
zeroColIndex.add(j);
20+
}
21+
}
22+
}
23+
24+
for (let i = 0; i < matrix.length; ++i) {
25+
for (let j = 0; j < matrix[i].length; ++j) {
26+
if (zeroRowIndex.has(i) || zeroColIndex.has(j)) {
27+
matrix[i][j] = 0;
28+
}
29+
}
30+
}
31+
32+
return matrix;
33+
};

LC-73/README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# LC-73 - Set Matrix Zeroes
2+
3+
Given an `m x n` integer matrix `matrix`, if an element is `0`, set its entire row and column to `0`'s.
4+
5+
You must do it in place.
6+
7+
> * Difficulty: **MEDIUM**
8+
9+
---
10+
## Examples
11+
12+
![](../res/img/LC-73-1.jpeg)
13+
14+
```
15+
Input: matrix = [[1,1,1],[1,0,1],[1,1,1]]
16+
Output: [[1,0,1],[0,0,0],[1,0,1]]
17+
```
18+
19+
![](../res/img/LC-73-2.jpeg)
20+
21+
```
22+
Input: matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]
23+
Output: [[0,0,0,0],[0,4,5,0],[0,3,1,0]]
24+
```
25+
26+
---
27+
## Notes
28+
29+
- `m == matrix.length`
30+
- `n == matrix[0].length`
31+
- `1 <= m, n <= 200`
32+
- `-231 <= matrix[i][j] <= 231 - 1`
33+
34+
---
35+
## Follow Up
36+
37+
- A straightforward solution using $O(mn)$ space is probably a bad idea.
38+
- A simple improvement uses $O(m + n)$ space, but still not the best solution.
39+
- Could you devise a constant space solution?
40+
41+
---
42+
## Solutions
43+
44+
1. Additional Memory Approach
45+
- Time complexity: $O(mn)$
46+
- Space complexity: $O(m + n)$
47+

res/img/LC-73-1.jpeg

13.5 KB
Loading

res/img/LC-73-2.jpeg

21.1 KB
Loading

0 commit comments

Comments
 (0)