Skip to content

feat: update solutions to lc problems: No.412,416 #3022

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 46 additions & 26 deletions solution/0400-0499/0412.Fizz Buzz/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:模拟

我们遍历从 1 到 n 的每个整数,对于每个整数,我们检查它是否是 3 和 5 的倍数,或者只是 3 的倍数,或者只是 5 的倍数。根据检查的结果,我们将相应的字符串添加到答案数组中。

时间复杂度 $O(n)$,其中 $n$ 是题目给定的整数。忽略答案数组的空间消耗,空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand Down Expand Up @@ -118,9 +122,15 @@ public:
vector<string> ans;
for (int i = 1; i <= n; ++i) {
string s = "";
if (i % 3 == 0) s += "Fizz";
if (i % 5 == 0) s += "Buzz";
if (s.size() == 0) s = to_string(i);
if (i % 3 == 0) {
s += "Fizz";
}
if (i % 5 == 0) {
s += "Buzz";
}
if (s.empty()) {
s = to_string(i);
}
ans.push_back(s);
}
return ans;
Expand All @@ -131,8 +141,7 @@ public:
#### Go

```go
func fizzBuzz(n int) []string {
var ans []string
func fizzBuzz(n int) (ans []string) {
for i := 1; i <= n; i++ {
s := &strings.Builder{}
if i%3 == 0 {
Expand All @@ -146,22 +155,31 @@ func fizzBuzz(n int) []string {
}
ans = append(ans, s.String())
}
return ans
return
}
```

#### JavaScript

```js
const fizzBuzz = function (n) {
let arr = [];
for (let i = 1; i <= n; i++) {
if (i % 15 === 0) arr.push('FizzBuzz');
else if (i % 3 === 0) arr.push('Fizz');
else if (i % 5 === 0) arr.push('Buzz');
else arr.push(`${i}`);
/**
* @param {number} n
* @return {string[]}
*/
var fizzBuzz = function (n) {
const ans = [];
for (let i = 1; i <= n; ++i) {
if (i % 15 === 0) {
ans.push('FizzBuzz');
} else if (i % 3 === 0) {
ans.push('Fizz');
} else if (i % 5 === 0) {
ans.push('Buzz');
} else {
ans.push(`${i}`);
}
}
return arr;
return ans;
};
```

Expand All @@ -174,19 +192,21 @@ class Solution {
* @return String[]
*/
function fizzBuzz($n) {
$rs = [];
for ($i = 1; $i <= $n; $i++) {
if ($i % 3 != 0 && $i % 5 != 0) {
array_push($rs, strval($i));
} elseif ($i % 3 == 0 && $i % 5 != 0) {
array_push($rs, 'Fizz');
} elseif ($i % 3 != 0 && $i % 5 == 0) {
array_push($rs, 'Buzz');
} else {
array_push($rs, 'FizzBuzz');
$ans = [];
for ($i = 1; $i <= $n; ++$i) {
$s = '';
if ($i % 3 == 0) {
$s .= 'Fizz';
}
if ($i % 5 == 0) {
$s .= 'Buzz';
}
if (strlen($s) == 0) {
$s .= $i;
}
$ans[] = $s;
}
return $rs;
return $ans;
}
}
```
Expand Down
72 changes: 46 additions & 26 deletions solution/0400-0499/0412.Fizz Buzz/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Simulation

We iterate through each integer from 1 to $n$. For each integer, we check whether it is a multiple of both 3 and 5, or just a multiple of 3, or just a multiple of 5. Based on the check result, we add the corresponding string to the answer array.

The time complexity is $O(n)$, where $n$ is the integer given in the problem. Ignoring the space consumption of the answer array, the space complexity is $O(1)$.

<!-- tabs:start -->

Expand Down Expand Up @@ -106,9 +110,15 @@ public:
vector<string> ans;
for (int i = 1; i <= n; ++i) {
string s = "";
if (i % 3 == 0) s += "Fizz";
if (i % 5 == 0) s += "Buzz";
if (s.size() == 0) s = to_string(i);
if (i % 3 == 0) {
s += "Fizz";
}
if (i % 5 == 0) {
s += "Buzz";
}
if (s.empty()) {
s = to_string(i);
}
ans.push_back(s);
}
return ans;
Expand All @@ -119,8 +129,7 @@ public:
#### Go

```go
func fizzBuzz(n int) []string {
var ans []string
func fizzBuzz(n int) (ans []string) {
for i := 1; i <= n; i++ {
s := &strings.Builder{}
if i%3 == 0 {
Expand All @@ -134,22 +143,31 @@ func fizzBuzz(n int) []string {
}
ans = append(ans, s.String())
}
return ans
return
}
```

#### JavaScript

```js
const fizzBuzz = function (n) {
let arr = [];
for (let i = 1; i <= n; i++) {
if (i % 15 === 0) arr.push('FizzBuzz');
else if (i % 3 === 0) arr.push('Fizz');
else if (i % 5 === 0) arr.push('Buzz');
else arr.push(`${i}`);
/**
* @param {number} n
* @return {string[]}
*/
var fizzBuzz = function (n) {
const ans = [];
for (let i = 1; i <= n; ++i) {
if (i % 15 === 0) {
ans.push('FizzBuzz');
} else if (i % 3 === 0) {
ans.push('Fizz');
} else if (i % 5 === 0) {
ans.push('Buzz');
} else {
ans.push(`${i}`);
}
}
return arr;
return ans;
};
```

Expand All @@ -162,19 +180,21 @@ class Solution {
* @return String[]
*/
function fizzBuzz($n) {
$rs = [];
for ($i = 1; $i <= $n; $i++) {
if ($i % 3 != 0 && $i % 5 != 0) {
array_push($rs, strval($i));
} elseif ($i % 3 == 0 && $i % 5 != 0) {
array_push($rs, 'Fizz');
} elseif ($i % 3 != 0 && $i % 5 == 0) {
array_push($rs, 'Buzz');
} else {
array_push($rs, 'FizzBuzz');
$ans = [];
for ($i = 1; $i <= $n; ++$i) {
$s = '';
if ($i % 3 == 0) {
$s .= 'Fizz';
}
if ($i % 5 == 0) {
$s .= 'Buzz';
}
if (strlen($s) == 0) {
$s .= $i;
}
$ans[] = $s;
}
return $rs;
return $ans;
}
}
```
Expand Down
12 changes: 9 additions & 3 deletions solution/0400-0499/0412.Fizz Buzz/Solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@ class Solution {
vector<string> ans;
for (int i = 1; i <= n; ++i) {
string s = "";
if (i % 3 == 0) s += "Fizz";
if (i % 5 == 0) s += "Buzz";
if (s.size() == 0) s = to_string(i);
if (i % 3 == 0) {
s += "Fizz";
}
if (i % 5 == 0) {
s += "Buzz";
}
if (s.empty()) {
s = to_string(i);
}
ans.push_back(s);
}
return ans;
Expand Down
5 changes: 2 additions & 3 deletions solution/0400-0499/0412.Fizz Buzz/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
func fizzBuzz(n int) []string {
var ans []string
func fizzBuzz(n int) (ans []string) {
for i := 1; i <= n; i++ {
s := &strings.Builder{}
if i%3 == 0 {
Expand All @@ -13,5 +12,5 @@ func fizzBuzz(n int) []string {
}
ans = append(ans, s.String())
}
return ans
return
}
25 changes: 17 additions & 8 deletions solution/0400-0499/0412.Fizz Buzz/Solution.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
const fizzBuzz = function (n) {
let arr = [];
for (let i = 1; i <= n; i++) {
if (i % 15 === 0) arr.push('FizzBuzz');
else if (i % 3 === 0) arr.push('Fizz');
else if (i % 5 === 0) arr.push('Buzz');
else arr.push(`${i}`);
/**
* @param {number} n
* @return {string[]}
*/
var fizzBuzz = function (n) {
const ans = [];
for (let i = 1; i <= n; ++i) {
if (i % 15 === 0) {
ans.push('FizzBuzz');
} else if (i % 3 === 0) {
ans.push('Fizz');
} else if (i % 5 === 0) {
ans.push('Buzz');
} else {
ans.push(`${i}`);
}
}
return arr;
return ans;
};
26 changes: 14 additions & 12 deletions solution/0400-0499/0412.Fizz Buzz/Solution.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@ class Solution {
* @return String[]
*/
function fizzBuzz($n) {
$rs = [];
for ($i = 1; $i <= $n; $i++) {
if ($i % 3 != 0 && $i % 5 != 0) {
array_push($rs, strval($i));
} elseif ($i % 3 == 0 && $i % 5 != 0) {
array_push($rs, 'Fizz');
} elseif ($i % 3 != 0 && $i % 5 == 0) {
array_push($rs, 'Buzz');
} else {
array_push($rs, 'FizzBuzz');
$ans = [];
for ($i = 1; $i <= $n; ++$i) {
$s = '';
if ($i % 3 == 0) {
$s .= 'Fizz';
}
if ($i % 5 == 0) {
$s .= 'Buzz';
}
if (strlen($s) == 0) {
$s .= $i;
}
$ans[] = $s;
}
return $rs;
return $ans;
}
}
}
35 changes: 4 additions & 31 deletions solution/0400-0499/0413.Arithmetic Slices/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ tags:

我们用 $d$ 表示当前相邻两个元素的差值,用 $cnt$ 表示当前等差数列的长度,初始时 $d = 3000$, $cnt = 2$。

遍历数组 `nums`,对于相邻的两个元素 $a$ 和 $b$,如果 $b - a = d$,则说明当前元素 $b$ 也属于当前等差数列,此时 $cnt$ 自增 1;否则说明当前元素 $b$ 不属于当前等差数列,此时更新 $d = b - a$,$cnt = 2$。如果 $cnt \ge 3$,则说明当前等差数列的长度至少为 3,此时等差数列的个数为 $cnt - 2$,将其加到答案中。
遍历数组 `nums`,对于相邻的两个元素 $a$ 和 $b$,如果 $b - a = d$,则说明当前元素 $b$ 也属于当前等差数列,此时 $cnt$ 自增 1;否则说明当前元素 $b$ 不属于当前等差数列,此时更新 $d = b - a$,$cnt = 2$。如果 $cnt \ge 3$,则说明当前等差数列的长度至少为 3,此时等差数列的个数为 $cnt - 2$,将其加到答案中。

遍历结束后,即可得到答案。

Expand All @@ -87,15 +87,15 @@ tags:
```python
class Solution:
def numberOfArithmeticSlices(self, nums: List[int]) -> int:
ans, cnt = 0, 2
ans = cnt = 0
d = 3000
for a, b in pairwise(nums):
if b - a == d:
cnt += 1
else:
d = b - a
cnt = 2
ans += max(0, cnt - 2)
cnt = 0
ans += cnt
return ans
```

Expand Down Expand Up @@ -187,31 +187,4 @@ function numberOfArithmeticSlices(nums: number[]): number {

<!-- solution:end -->

<!-- solution:start -->

### 方法二

<!-- tabs:start -->

#### Python3

```python
class Solution:
def numberOfArithmeticSlices(self, nums: List[int]) -> int:
ans = cnt = 0
d = 3000
for a, b in pairwise(nums):
if b - a == d:
cnt += 1
else:
d = b - a
cnt = 0
ans += cnt
return ans
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Loading
Loading