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 a470c51

Browse files
feat: add solutions to lc problem: No.3028 (doocs#2315)
No.3028.Ant on the Boundary
1 parent cbbb894 commit a470c51

File tree

7 files changed

+146
-8
lines changed

7 files changed

+146
-8
lines changed

‎solution/3000-3099/3028.Ant on the Boundary/README.md‎

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,24 +61,71 @@
6161

6262
## 解法
6363

64-
### 方法一
64+
### 方法一:前缀和
65+
66+
根据题目描述,我们只需要计算 $nums$ 的所有前缀和中有多少个 0ドル$ 即可。
67+
68+
时间复杂度 $O(n),ドル其中 $n$ 为 $nums$ 的长度。空间复杂度 $O(1)$。
6569

6670
<!-- tabs:start -->
6771

6872
```python
69-
73+
class Solution:
74+
def returnToBoundaryCount(self, nums: List[int]) -> int:
75+
return sum(s == 0 for s in accumulate(nums))
7076
```
7177

7278
```java
73-
79+
class Solution {
80+
public int returnToBoundaryCount(int[] nums) {
81+
int ans = 0, s = 0;
82+
for (int x : nums) {
83+
s += x;
84+
if (s == 0) {
85+
++ans;
86+
}
87+
}
88+
return ans;
89+
}
90+
}
7491
```
7592

7693
```cpp
77-
94+
class Solution {
95+
public:
96+
int returnToBoundaryCount(vector<int>& nums) {
97+
int ans = 0, s = 0;
98+
for (int x : nums) {
99+
s += x;
100+
ans += s == 0;
101+
}
102+
return ans;
103+
}
104+
};
78105
```
79106
80107
```go
108+
func returnToBoundaryCount(nums []int) (ans int) {
109+
s := 0
110+
for _, x := range nums {
111+
s += x
112+
if s == 0 {
113+
ans++
114+
}
115+
}
116+
return
117+
}
118+
```
81119

120+
```ts
121+
function returnToBoundaryCount(nums: number[]): number {
122+
let [ans, s] = [0, 0];
123+
for (const x of nums) {
124+
s += x;
125+
ans += s === 0 ? 1 : 0;
126+
}
127+
return ans;
128+
}
82129
```
83130

84131
<!-- tabs:end -->

‎solution/3000-3099/3028.Ant on the Boundary/README_EN.md‎

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,24 +57,71 @@ The ant never returned to the boundary, so the answer is 0.
5757

5858
## Solutions
5959

60-
### Solution 1
60+
### Solution 1: Prefix Sum
61+
62+
Based on the problem description, we only need to calculate how many zeros are in all prefix sums of `nums`.
63+
64+
The time complexity is $O(n),ドル where $n$ is the length of `nums`. The space complexity is $O(1)$.
6165

6266
<!-- tabs:start -->
6367

6468
```python
65-
69+
class Solution:
70+
def returnToBoundaryCount(self, nums: List[int]) -> int:
71+
return sum(s == 0 for s in accumulate(nums))
6672
```
6773

6874
```java
69-
75+
class Solution {
76+
public int returnToBoundaryCount(int[] nums) {
77+
int ans = 0, s = 0;
78+
for (int x : nums) {
79+
s += x;
80+
if (s == 0) {
81+
++ans;
82+
}
83+
}
84+
return ans;
85+
}
86+
}
7087
```
7188

7289
```cpp
73-
90+
class Solution {
91+
public:
92+
int returnToBoundaryCount(vector<int>& nums) {
93+
int ans = 0, s = 0;
94+
for (int x : nums) {
95+
s += x;
96+
ans += s == 0;
97+
}
98+
return ans;
99+
}
100+
};
74101
```
75102
76103
```go
104+
func returnToBoundaryCount(nums []int) (ans int) {
105+
s := 0
106+
for _, x := range nums {
107+
s += x
108+
if s == 0 {
109+
ans++
110+
}
111+
}
112+
return
113+
}
114+
```
77115

116+
```ts
117+
function returnToBoundaryCount(nums: number[]): number {
118+
let [ans, s] = [0, 0];
119+
for (const x of nums) {
120+
s += x;
121+
ans += s === 0 ? 1 : 0;
122+
}
123+
return ans;
124+
}
78125
```
79126

80127
<!-- tabs:end -->
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public:
3+
int returnToBoundaryCount(vector<int>& nums) {
4+
int ans = 0, s = 0;
5+
for (int x : nums) {
6+
s += x;
7+
ans += s == 0;
8+
}
9+
return ans;
10+
}
11+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
func returnToBoundaryCount(nums []int) (ans int) {
2+
s := 0
3+
for _, x := range nums {
4+
s += x
5+
if s == 0 {
6+
ans++
7+
}
8+
}
9+
return
10+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public int returnToBoundaryCount(int[] nums) {
3+
int ans = 0, s = 0;
4+
for (int x : nums) {
5+
s += x;
6+
if (s == 0) {
7+
++ans;
8+
}
9+
}
10+
return ans;
11+
}
12+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def returnToBoundaryCount(self, nums: List[int]) -> int:
3+
return sum(s == 0 for s in accumulate(nums))
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function returnToBoundaryCount(nums: number[]): number {
2+
let [ans, s] = [0, 0];
3+
for (const x of nums) {
4+
s += x;
5+
ans += s === 0 ? 1 : 0;
6+
}
7+
return ans;
8+
}

0 commit comments

Comments
(0)

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