diff --git a/solution/3000-3099/3028.Ant on the Boundary/README.md b/solution/3000-3099/3028.Ant on the Boundary/README.md index b46cec05f43d5..3a6e73e02ac59 100644 --- a/solution/3000-3099/3028.Ant on the Boundary/README.md +++ b/solution/3000-3099/3028.Ant on the Boundary/README.md @@ -61,24 +61,71 @@ ## 解法 -### 方法一 +### 方法一:前缀和 + +根据题目描述,我们只需要计算 $nums$ 的所有前缀和中有多少个 $0$ 即可。 + +时间复杂度 $O(n)$,其中 $n$ 为 $nums$ 的长度。空间复杂度 $O(1)$。 ```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& 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; +} ``` diff --git a/solution/3000-3099/3028.Ant on the Boundary/README_EN.md b/solution/3000-3099/3028.Ant on the Boundary/README_EN.md index f8efe7739bda0..2eba8f0f1c645 100644 --- a/solution/3000-3099/3028.Ant on the Boundary/README_EN.md +++ b/solution/3000-3099/3028.Ant on the Boundary/README_EN.md @@ -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)$. ```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& 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; +} ``` diff --git a/solution/3000-3099/3028.Ant on the Boundary/Solution.cpp b/solution/3000-3099/3028.Ant on the Boundary/Solution.cpp new file mode 100644 index 0000000000000..33b9f5dd7581b --- /dev/null +++ b/solution/3000-3099/3028.Ant on the Boundary/Solution.cpp @@ -0,0 +1,11 @@ +class Solution { +public: + int returnToBoundaryCount(vector& nums) { + int ans = 0, s = 0; + for (int x : nums) { + s += x; + ans += s == 0; + } + return ans; + } +}; \ No newline at end of file diff --git a/solution/3000-3099/3028.Ant on the Boundary/Solution.go b/solution/3000-3099/3028.Ant on the Boundary/Solution.go new file mode 100644 index 0000000000000..44791810ff9c2 --- /dev/null +++ b/solution/3000-3099/3028.Ant on the Boundary/Solution.go @@ -0,0 +1,10 @@ +func returnToBoundaryCount(nums []int) (ans int) { + s := 0 + for _, x := range nums { + s += x + if s == 0 { + ans++ + } + } + return +} \ No newline at end of file diff --git a/solution/3000-3099/3028.Ant on the Boundary/Solution.java b/solution/3000-3099/3028.Ant on the Boundary/Solution.java new file mode 100644 index 0000000000000..539d9b58acff5 --- /dev/null +++ b/solution/3000-3099/3028.Ant on the Boundary/Solution.java @@ -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; + } +} \ No newline at end of file diff --git a/solution/3000-3099/3028.Ant on the Boundary/Solution.py b/solution/3000-3099/3028.Ant on the Boundary/Solution.py new file mode 100644 index 0000000000000..d43697938cb73 --- /dev/null +++ b/solution/3000-3099/3028.Ant on the Boundary/Solution.py @@ -0,0 +1,3 @@ +class Solution: + def returnToBoundaryCount(self, nums: List[int]) -> int: + return sum(s == 0 for s in accumulate(nums)) diff --git a/solution/3000-3099/3028.Ant on the Boundary/Solution.ts b/solution/3000-3099/3028.Ant on the Boundary/Solution.ts new file mode 100644 index 0000000000000..d141a46482383 --- /dev/null +++ b/solution/3000-3099/3028.Ant on the Boundary/Solution.ts @@ -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; +}