Skip to content
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

feat: add solutions to lc problem: No.3028 #2315

Merged
merged 1 commit into from
Feb 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
55 changes: 51 additions & 4 deletions solution/3000-3099/3028.Ant on the Boundary/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,71 @@

## 解法

### 方法一
### 方法一:前缀和

根据题目描述,我们只需要计算 $nums$ 的所有前缀和中有多少个 $0$ 即可。

时间复杂度 $O(n)$,其中 $n$ 为 $nums$ 的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

```python

class Solution:
def returnToBoundaryCount(self, nums: List[int]) -> int:
return sum(s == 0 for s in accumulate(nums))
```

```java

class Solution {
public int returnToBoundaryCount(int[] nums) {
int ans = 0, s = 0;
for (int x : nums) {
s += x;
if (s == 0) {
++ans;
}
}
return ans;
}
}
```

```cpp

class Solution {
public:
int returnToBoundaryCount(vector<int>& nums) {
int ans = 0, s = 0;
for (int x : nums) {
s += x;
ans += s == 0;
}
return ans;
}
};
```

```go
func returnToBoundaryCount(nums []int) (ans int) {
s := 0
for _, x := range nums {
s += x
if s == 0 {
ans++
}
}
return
}
```

```ts
function returnToBoundaryCount(nums: number[]): number {
let [ans, s] = [0, 0];
for (const x of nums) {
s += x;
ans += s === 0 ? 1 : 0;
}
return ans;
}
```

<!-- tabs:end -->
Expand Down
55 changes: 51 additions & 4 deletions solution/3000-3099/3028.Ant on the Boundary/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,71 @@ The ant never returned to the boundary, so the answer is 0.

## Solutions

### Solution 1
### Solution 1: Prefix Sum

Based on the problem description, we only need to calculate how many zeros are in all prefix sums of `nums`.

The time complexity is $O(n)$, where $n$ is the length of `nums`. The space complexity is $O(1)$.

<!-- tabs:start -->

```python

class Solution:
def returnToBoundaryCount(self, nums: List[int]) -> int:
return sum(s == 0 for s in accumulate(nums))
```

```java

class Solution {
public int returnToBoundaryCount(int[] nums) {
int ans = 0, s = 0;
for (int x : nums) {
s += x;
if (s == 0) {
++ans;
}
}
return ans;
}
}
```

```cpp

class Solution {
public:
int returnToBoundaryCount(vector<int>& nums) {
int ans = 0, s = 0;
for (int x : nums) {
s += x;
ans += s == 0;
}
return ans;
}
};
```

```go
func returnToBoundaryCount(nums []int) (ans int) {
s := 0
for _, x := range nums {
s += x
if s == 0 {
ans++
}
}
return
}
```

```ts
function returnToBoundaryCount(nums: number[]): number {
let [ans, s] = [0, 0];
for (const x of nums) {
s += x;
ans += s === 0 ? 1 : 0;
}
return ans;
}
```

<!-- tabs:end -->
Expand Down
11 changes: 11 additions & 0 deletions solution/3000-3099/3028.Ant on the Boundary/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution {
public:
int returnToBoundaryCount(vector<int>& nums) {
int ans = 0, s = 0;
for (int x : nums) {
s += x;
ans += s == 0;
}
return ans;
}
};
10 changes: 10 additions & 0 deletions solution/3000-3099/3028.Ant on the Boundary/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
func returnToBoundaryCount(nums []int) (ans int) {
s := 0
for _, x := range nums {
s += x
if s == 0 {
ans++
}
}
return
}
12 changes: 12 additions & 0 deletions solution/3000-3099/3028.Ant on the Boundary/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution {
public int returnToBoundaryCount(int[] nums) {
int ans = 0, s = 0;
for (int x : nums) {
s += x;
if (s == 0) {
++ans;
}
}
return ans;
}
}
3 changes: 3 additions & 0 deletions solution/3000-3099/3028.Ant on the Boundary/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Solution:
def returnToBoundaryCount(self, nums: List[int]) -> int:
return sum(s == 0 for s in accumulate(nums))
8 changes: 8 additions & 0 deletions solution/3000-3099/3028.Ant on the Boundary/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function returnToBoundaryCount(nums: number[]): number {
let [ans, s] = [0, 0];
for (const x of nums) {
s += x;
ans += s === 0 ? 1 : 0;
}
return ans;
}