From b4dd15114edaa0269ae292f60d7f178176cf87d0 Mon Sep 17 00:00:00 2001 From: chriy Date: Sat, 21 May 2022 10:36:54 +0800 Subject: [PATCH 1/3] feat: add solutions to lc problem: No.2269 --- .../README.md | 24 +++++++++++++++++-- .../README_EN.md | 24 +++++++++++++++++-- .../Solution.java | 13 ++++++++++ .../Solution.py | 9 +++++++ 4 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 solution/2200-2299/2269.Find the K-Beauty of a Number/Solution.java create mode 100644 solution/2200-2299/2269.Find the K-Beauty of a Number/Solution.py diff --git a/solution/2200-2299/2269.Find the K-Beauty of a Number/README.md b/solution/2200-2299/2269.Find the K-Beauty of a Number/README.md index 65006061026e0..1f4831844c7a0 100644 --- a/solution/2200-2299/2269.Find the K-Beauty of a Number/README.md +++ b/solution/2200-2299/2269.Find the K-Beauty of a Number/README.md @@ -71,7 +71,15 @@ ```python - +class Solution: + def divisorSubstrings(self, num: int, k: int) -> int: + cnt = 0 + s = str(num) + for i in range(len(s) - k + 1): + tmp = int(s[i: i + k]) + if tmp != 0 and num % tmp == 0: + cnt += 1 + return cnt ``` ### **Java** @@ -79,7 +87,19 @@ ```java - +class Solution { + public int divisorSubstrings(int num, int k) { + int cnt = 0; + String s = String.valueOf(num); + for(int i = 0; i <= s.length() - k; i++){ + int tmp = Integer.parseInt(s.substring(i, i + k)); + if(tmp != 0 && num % tmp == 0){ + cnt++; + } + } + return cnt; + } +} ``` ### **TypeScript** diff --git a/solution/2200-2299/2269.Find the K-Beauty of a Number/README_EN.md b/solution/2200-2299/2269.Find the K-Beauty of a Number/README_EN.md index 8a5156d87baa0..a70e4c49e38d6 100644 --- a/solution/2200-2299/2269.Find the K-Beauty of a Number/README_EN.md +++ b/solution/2200-2299/2269.Find the K-Beauty of a Number/README_EN.md @@ -63,13 +63,33 @@ Therefore, the k-beauty is 2. ### **Python3** ```python - +class Solution: + def divisorSubstrings(self, num: int, k: int) -> int: + cnt = 0 + s = str(num) + for i in range(len(s) - k + 1): + tmp = int(s[i: i + k]) + if tmp != 0 and num % tmp == 0: + cnt += 1 + return cnt ``` ### **Java** ```java - +class Solution { + public int divisorSubstrings(int num, int k) { + int cnt = 0; + String s = String.valueOf(num); + for(int i = 0; i <= s.length() - k; i++){ + int tmp = Integer.parseInt(s.substring(i, i + k)); + if(tmp != 0 && num % tmp == 0){ + cnt++; + } + } + return cnt; + } +} ``` ### **TypeScript** diff --git a/solution/2200-2299/2269.Find the K-Beauty of a Number/Solution.java b/solution/2200-2299/2269.Find the K-Beauty of a Number/Solution.java new file mode 100644 index 0000000000000..85f2bea98a769 --- /dev/null +++ b/solution/2200-2299/2269.Find the K-Beauty of a Number/Solution.java @@ -0,0 +1,13 @@ +class Solution { + public int divisorSubstrings(int num, int k) { + int cnt = 0; + String s = String.valueOf(num); + for(int i = 0; i <= s.length() - k; i++){ + int tmp = Integer.parseInt(s.substring(i, i + k)); + if(tmp != 0 && num % tmp == 0){ + cnt++; + } + } + return cnt; + } +} \ No newline at end of file diff --git a/solution/2200-2299/2269.Find the K-Beauty of a Number/Solution.py b/solution/2200-2299/2269.Find the K-Beauty of a Number/Solution.py new file mode 100644 index 0000000000000..e052711b4c024 --- /dev/null +++ b/solution/2200-2299/2269.Find the K-Beauty of a Number/Solution.py @@ -0,0 +1,9 @@ +class Solution: + def divisorSubstrings(self, num: int, k: int) -> int: + cnt = 0 + s = str(num) + for i in range(len(s) - k + 1): + tmp = int(s[i: i + k]) + if tmp != 0 and num % tmp == 0: + cnt += 1 + return cnt \ No newline at end of file From 9f4b2b93608aae0ba1098cbf46cc237339c67709 Mon Sep 17 00:00:00 2001 From: chriy Date: Sat, 21 May 2022 10:57:32 +0800 Subject: [PATCH 2/3] feat: add solutions to lc problem: No.2270 --- .../README.md | 27 +++++++++++++++++-- .../README_EN.md | 27 +++++++++++++++++-- .../Solution.java | 15 +++++++++++ .../Solution.py | 10 +++++++ 4 files changed, 75 insertions(+), 4 deletions(-) create mode 100644 solution/2200-2299/2270.Number of Ways to Split Array/Solution.java create mode 100644 solution/2200-2299/2270.Number of Ways to Split Array/Solution.py diff --git a/solution/2200-2299/2270.Number of Ways to Split Array/README.md b/solution/2200-2299/2270.Number of Ways to Split Array/README.md index 990667c240255..8d3e4683461ec 100644 --- a/solution/2200-2299/2270.Number of Ways to Split Array/README.md +++ b/solution/2200-2299/2270.Number of Ways to Split Array/README.md @@ -62,7 +62,16 @@ ```python - +class Solution: + def waysToSplitArray(self, nums: List[int]) -> int: + n, left, right = len(nums), 0, sum(nums) + cnt = 0 + for i in range(n - 1): + left += nums[i] + right -= nums[i] + if left >= right: + cnt += 1 + return cnt ``` ### **Java** @@ -70,7 +79,21 @@ ```java - +class Solution { + public int waysToSplitArray(int[] nums) { + long[] pre = new long[nums.length + 1]; + for (int i = 0; i < nums.length; i++) { + pre[i + 1] = pre[i] + nums[i]; + } + int cnt = 0; + for (int i = 1; i < nums.length; i++) { + if (pre[i] >= pre[nums.length] - pre[i]) { + cnt++; + } + } + return cnt; + } +} ``` ### **TypeScript** diff --git a/solution/2200-2299/2270.Number of Ways to Split Array/README_EN.md b/solution/2200-2299/2270.Number of Ways to Split Array/README_EN.md index 4e64f49ba044b..914069107e209 100644 --- a/solution/2200-2299/2270.Number of Ways to Split Array/README_EN.md +++ b/solution/2200-2299/2270.Number of Ways to Split Array/README_EN.md @@ -55,13 +55,36 @@ There are two valid splits in nums: ### **Python3** ```python - +class Solution: + def waysToSplitArray(self, nums: List[int]) -> int: + n, left, right = len(nums), 0, sum(nums) + cnt = 0 + for i in range(n - 1): + left += nums[i] + right -= nums[i] + if left >= right: + cnt += 1 + return cnt ``` ### **Java** ```java - +class Solution { + public int waysToSplitArray(int[] nums) { + long[] pre = new long[nums.length + 1]; + for (int i = 0; i < nums.length; i++) { + pre[i + 1] = pre[i] + nums[i]; + } + int cnt = 0; + for (int i = 1; i < nums.length; i++) { + if (pre[i] >= pre[nums.length] - pre[i]) { + cnt++; + } + } + return cnt; + } +} ``` ### **TypeScript** diff --git a/solution/2200-2299/2270.Number of Ways to Split Array/Solution.java b/solution/2200-2299/2270.Number of Ways to Split Array/Solution.java new file mode 100644 index 0000000000000..36124d5c9ffbd --- /dev/null +++ b/solution/2200-2299/2270.Number of Ways to Split Array/Solution.java @@ -0,0 +1,15 @@ +class Solution { + public int waysToSplitArray(int[] nums) { + long[] pre = new long[nums.length + 1]; + for (int i = 0; i < nums.length; i++) { + pre[i + 1] = pre[i] + nums[i]; + } + int cnt = 0; + for (int i = 1; i < nums.length; i++) { + if (pre[i] >= pre[nums.length] - pre[i]) { + cnt++; + } + } + return cnt; + } +} \ No newline at end of file diff --git a/solution/2200-2299/2270.Number of Ways to Split Array/Solution.py b/solution/2200-2299/2270.Number of Ways to Split Array/Solution.py new file mode 100644 index 0000000000000..f17294e2db4b4 --- /dev/null +++ b/solution/2200-2299/2270.Number of Ways to Split Array/Solution.py @@ -0,0 +1,10 @@ +class Solution: + def waysToSplitArray(self, nums: List[int]) -> int: + n, left, right = len(nums), 0, sum(nums) + cnt = 0 + for i in range(n - 1): + left += nums[i] + right -= nums[i] + if left >= right: + cnt += 1 + return cnt \ No newline at end of file From ad10f99521da05cee41ff0e0364587ad87fe1f3f Mon Sep 17 00:00:00 2001 From: chriy Date: Sat, 21 May 2022 11:28:22 +0800 Subject: [PATCH 3/3] feat: update solutions to lc problems * No.2269.Find the K-Beauty of a Number * No.2270.Number of Ways to Split Array --- .../2269.Find the K-Beauty of a Number/README.md | 4 ++-- .../2269.Find the K-Beauty of a Number/README_EN.md | 4 ++-- .../2269.Find the K-Beauty of a Number/Solution.java | 4 ++-- .../2270.Number of Ways to Split Array/README.md | 8 ++++---- .../2270.Number of Ways to Split Array/README_EN.md | 8 ++++---- .../2270.Number of Ways to Split Array/Solution.py | 8 ++++---- 6 files changed, 18 insertions(+), 18 deletions(-) diff --git a/solution/2200-2299/2269.Find the K-Beauty of a Number/README.md b/solution/2200-2299/2269.Find the K-Beauty of a Number/README.md index 1f4831844c7a0..313a8cc79e576 100644 --- a/solution/2200-2299/2269.Find the K-Beauty of a Number/README.md +++ b/solution/2200-2299/2269.Find the K-Beauty of a Number/README.md @@ -91,9 +91,9 @@ class Solution { public int divisorSubstrings(int num, int k) { int cnt = 0; String s = String.valueOf(num); - for(int i = 0; i <= s.length() - k; i++){ + for (int i = 0; i <= s.length() - k; i++) { int tmp = Integer.parseInt(s.substring(i, i + k)); - if(tmp != 0 && num % tmp == 0){ + if (tmp != 0 && num % tmp == 0) { cnt++; } } diff --git a/solution/2200-2299/2269.Find the K-Beauty of a Number/README_EN.md b/solution/2200-2299/2269.Find the K-Beauty of a Number/README_EN.md index a70e4c49e38d6..0fa9601f85c85 100644 --- a/solution/2200-2299/2269.Find the K-Beauty of a Number/README_EN.md +++ b/solution/2200-2299/2269.Find the K-Beauty of a Number/README_EN.md @@ -81,9 +81,9 @@ class Solution { public int divisorSubstrings(int num, int k) { int cnt = 0; String s = String.valueOf(num); - for(int i = 0; i <= s.length() - k; i++){ + for (int i = 0; i <= s.length() - k; i++) { int tmp = Integer.parseInt(s.substring(i, i + k)); - if(tmp != 0 && num % tmp == 0){ + if (tmp != 0 && num % tmp == 0) { cnt++; } } diff --git a/solution/2200-2299/2269.Find the K-Beauty of a Number/Solution.java b/solution/2200-2299/2269.Find the K-Beauty of a Number/Solution.java index 85f2bea98a769..97b8e29b40177 100644 --- a/solution/2200-2299/2269.Find the K-Beauty of a Number/Solution.java +++ b/solution/2200-2299/2269.Find the K-Beauty of a Number/Solution.java @@ -2,9 +2,9 @@ class Solution { public int divisorSubstrings(int num, int k) { int cnt = 0; String s = String.valueOf(num); - for(int i = 0; i <= s.length() - k; i++){ + for (int i = 0; i <= s.length() - k; i++) { int tmp = Integer.parseInt(s.substring(i, i + k)); - if(tmp != 0 && num % tmp == 0){ + if (tmp != 0 && num % tmp == 0) { cnt++; } } diff --git a/solution/2200-2299/2270.Number of Ways to Split Array/README.md b/solution/2200-2299/2270.Number of Ways to Split Array/README.md index 8d3e4683461ec..5f00e259f3fd4 100644 --- a/solution/2200-2299/2270.Number of Ways to Split Array/README.md +++ b/solution/2200-2299/2270.Number of Ways to Split Array/README.md @@ -64,11 +64,11 @@ ```python class Solution: def waysToSplitArray(self, nums: List[int]) -> int: - n, left, right = len(nums), 0, sum(nums) + left, right = 0, sum(nums) cnt = 0 - for i in range(n - 1): - left += nums[i] - right -= nums[i] + for v in nums[:-1]: + left += v + right -= v if left >= right: cnt += 1 return cnt diff --git a/solution/2200-2299/2270.Number of Ways to Split Array/README_EN.md b/solution/2200-2299/2270.Number of Ways to Split Array/README_EN.md index 914069107e209..3d46f2d4992fc 100644 --- a/solution/2200-2299/2270.Number of Ways to Split Array/README_EN.md +++ b/solution/2200-2299/2270.Number of Ways to Split Array/README_EN.md @@ -57,11 +57,11 @@ There are two valid splits in nums: ```python class Solution: def waysToSplitArray(self, nums: List[int]) -> int: - n, left, right = len(nums), 0, sum(nums) + left, right = 0, sum(nums) cnt = 0 - for i in range(n - 1): - left += nums[i] - right -= nums[i] + for v in nums[:-1]: + left += v + right -= v if left >= right: cnt += 1 return cnt diff --git a/solution/2200-2299/2270.Number of Ways to Split Array/Solution.py b/solution/2200-2299/2270.Number of Ways to Split Array/Solution.py index f17294e2db4b4..454cf6d2f6532 100644 --- a/solution/2200-2299/2270.Number of Ways to Split Array/Solution.py +++ b/solution/2200-2299/2270.Number of Ways to Split Array/Solution.py @@ -1,10 +1,10 @@ class Solution: def waysToSplitArray(self, nums: List[int]) -> int: - n, left, right = len(nums), 0, sum(nums) + left, right = 0, sum(nums) cnt = 0 - for i in range(n - 1): - left += nums[i] - right -= nums[i] + for v in nums[:-1]: + left += v + right -= v if left >= right: cnt += 1 return cnt \ No newline at end of file