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.2269 & No.2270 #818

Merged
merged 3 commits into from
May 21, 2022
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
24 changes: 22 additions & 2 deletions solution/2200-2299/2269.Find the K-Beauty of a Number/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,35 @@
<!-- 这里可写当前语言的特殊实现逻辑 -->

```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**
Expand Down
24 changes: 22 additions & 2 deletions solution/2200-2299/2269.Find the K-Beauty of a Number/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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
27 changes: 25 additions & 2 deletions solution/2200-2299/2270.Number of Ways to Split Array/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,38 @@
<!-- 这里可写当前语言的特殊实现逻辑 -->

```python

class Solution:
def waysToSplitArray(self, nums: List[int]) -> int:
left, right = 0, sum(nums)
cnt = 0
for v in nums[:-1]:
left += v
right -= v
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**
Expand Down
27 changes: 25 additions & 2 deletions solution/2200-2299/2270.Number of Ways to Split Array/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,36 @@ There are two valid splits in nums:
### **Python3**

```python

class Solution:
def waysToSplitArray(self, nums: List[int]) -> int:
left, right = 0, sum(nums)
cnt = 0
for v in nums[:-1]:
left += v
right -= v
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**
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
10 changes: 10 additions & 0 deletions solution/2200-2299/2270.Number of Ways to Split Array/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Solution:
def waysToSplitArray(self, nums: List[int]) -> int:
left, right = 0, sum(nums)
cnt = 0
for v in nums[:-1]:
left += v
right -= v
if left >= right:
cnt += 1
return cnt