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 1c9ec93

Browse files
committed
feat: add solutions to lc problems: No.2243,2244
* No.2243.Calculate Digit Sum of a String * No.2244.Minimum Rounds to Complete All Tasks
1 parent 5c69b7c commit 1c9ec93

File tree

12 files changed

+276
-92
lines changed

12 files changed

+276
-92
lines changed

‎solution/2200-2299/2243.Calculate Digit Sum of a String/README.md‎

Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,33 @@ s 变为 "0" + "0" + "0" = "000" ,其长度等于 k ,所以返回 "000" 。
5858

5959
<!-- 这里可写通用的实现逻辑 -->
6060

61+
**方法一:模拟**
62+
63+
根据题意,我们可以模拟题目中的操作过程,直到字符串长度小于等于 $k$ 为止,最后返回字符串即可。
64+
65+
时间复杂度 $O(n),ドル空间复杂度 $O(n)$。其中 $n$ 为字符串 $s$ 的长度。
66+
6167
<!-- tabs:start -->
6268

6369
### **Python3**
6470

6571
<!-- 这里可写当前语言的特殊实现逻辑 -->
6672

73+
```python
74+
class Solution:
75+
def digitSum(self, s: str, k: int) -> str:
76+
while len(s) > k:
77+
t = []
78+
n = len(s)
79+
for i in range(0, n, k):
80+
x = 0
81+
for j in range(i, min(i + k, n)):
82+
x += int(s[j])
83+
t.append(str(x))
84+
s = "".join(t)
85+
return s
86+
```
87+
6788
```python
6889
class Solution:
6990
def digitSum(self, s: str, k: int) -> str:
@@ -85,21 +106,64 @@ class Solution {
85106
public String digitSum(String s, int k) {
86107
while (s.length() > k) {
87108
int n = s.length();
88-
StringBuilder sb = new StringBuilder();
109+
StringBuilder t = new StringBuilder();
89110
for (int i = 0; i < n; i += k) {
90-
int v = 0;
111+
int x = 0;
91112
for (int j = i; j < Math.min(i + k, n); ++j) {
92-
v += s.charAt(j) - '0';
113+
x += s.charAt(j) - '0';
93114
}
94-
sb.append(v +"");
115+
t.append(x);
95116
}
96-
s = sb.toString();
117+
s = t.toString();
97118
}
98119
return s;
99120
}
100121
}
101122
```
102123

124+
### **C++**
125+
126+
```cpp
127+
class Solution {
128+
public:
129+
string digitSum(string s, int k) {
130+
while (s.size() > k) {
131+
string t;
132+
int n = s.size();
133+
for (int i = 0; i < n; i += k) {
134+
int x = 0;
135+
for (int j = i; j < min(i + k, n); ++j) {
136+
x += s[j] - '0';
137+
}
138+
t += to_string(x);
139+
}
140+
s = t;
141+
}
142+
return s;
143+
}
144+
};
145+
```
146+
147+
### **Go**
148+
149+
```go
150+
func digitSum(s string, k int) string {
151+
for len(s) > k {
152+
t := &strings.Builder{}
153+
n := len(s)
154+
for i := 0; i < n; i += k {
155+
x := 0
156+
for j := i; j < i+k && j < n; j++ {
157+
x += int(s[j] - '0')
158+
}
159+
t.WriteString(strconv.Itoa(x))
160+
}
161+
s = t.String()
162+
}
163+
return s
164+
}
165+
```
166+
103167
### **TypeScript**
104168

105169
```ts

‎solution/2200-2299/2243.Calculate Digit Sum of a String/README_EN.md‎

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,21 @@ s becomes &quot;0&quot; + &quot;0&quot; + &quot;0&quot; = &quot;000&quot;, whose
5858

5959
### **Python3**
6060

61+
```python
62+
class Solution:
63+
def digitSum(self, s: str, k: int) -> str:
64+
while len(s) > k:
65+
t = []
66+
n = len(s)
67+
for i in range(0, n, k):
68+
x = 0
69+
for j in range(i, min(i + k, n)):
70+
x += int(s[j])
71+
t.append(str(x))
72+
s = "".join(t)
73+
return s
74+
```
75+
6176
```python
6277
class Solution:
6378
def digitSum(self, s: str, k: int) -> str:
@@ -77,18 +92,61 @@ class Solution {
7792
public String digitSum(String s, int k) {
7893
while (s.length() > k) {
7994
int n = s.length();
80-
StringBuilder sb = new StringBuilder();
95+
StringBuilder t = new StringBuilder();
8196
for (int i = 0; i < n; i += k) {
82-
int v = 0;
97+
int x = 0;
8398
for (int j = i; j < Math.min(i + k, n); ++j) {
84-
v += s.charAt(j) - '0';
99+
x += s.charAt(j) - '0';
100+
}
101+
t.append(x);
102+
}
103+
s = t.toString();
104+
}
105+
return s;
106+
}
107+
}
108+
```
109+
110+
### **C++**
111+
112+
```cpp
113+
class Solution {
114+
public:
115+
string digitSum(string s, int k) {
116+
while (s.size() > k) {
117+
string t;
118+
int n = s.size();
119+
for (int i = 0; i < n; i += k) {
120+
int x = 0;
121+
for (int j = i; j < min(i + k, n); ++j) {
122+
x += s[j] - '0';
85123
}
86-
sb.append(v +"");
124+
t += to_string(x);
87125
}
88-
s = sb.toString();
126+
s = t;
89127
}
90128
return s;
91129
}
130+
};
131+
```
132+
133+
### **Go**
134+
135+
```go
136+
func digitSum(s string, k int) string {
137+
for len(s) > k {
138+
t := &strings.Builder{}
139+
n := len(s)
140+
for i := 0; i < n; i += k {
141+
x := 0
142+
for j := i; j < i+k && j < n; j++ {
143+
x += int(s[j] - '0')
144+
}
145+
t.WriteString(strconv.Itoa(x))
146+
}
147+
s = t.String()
148+
}
149+
return s
92150
}
93151
```
94152

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
string digitSum(string s, int k) {
4+
while (s.size() > k) {
5+
string t;
6+
int n = s.size();
7+
for (int i = 0; i < n; i += k) {
8+
int x = 0;
9+
for (int j = i; j < min(i + k, n); ++j) {
10+
x += s[j] - '0';
11+
}
12+
t += to_string(x);
13+
}
14+
s = t;
15+
}
16+
return s;
17+
}
18+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
func digitSum(s string, k int) string {
2+
for len(s) > k {
3+
t := &strings.Builder{}
4+
n := len(s)
5+
for i := 0; i < n; i += k {
6+
x := 0
7+
for j := i; j < i+k && j < n; j++ {
8+
x += int(s[j] - '0')
9+
}
10+
t.WriteString(strconv.Itoa(x))
11+
}
12+
s = t.String()
13+
}
14+
return s
15+
}

‎solution/2200-2299/2243.Calculate Digit Sum of a String/Solution.java‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ class Solution {
22
public String digitSum(String s, int k) {
33
while (s.length() > k) {
44
int n = s.length();
5-
StringBuilder sb = new StringBuilder();
5+
StringBuilder t = new StringBuilder();
66
for (int i = 0; i < n; i += k) {
7-
int v = 0;
7+
int x = 0;
88
for (int j = i; j < Math.min(i + k, n); ++j) {
9-
v += s.charAt(j) - '0';
9+
x += s.charAt(j) - '0';
1010
}
11-
sb.append(v + "");
11+
t.append(x);
1212
}
13-
s = sb.toString();
13+
s = t.toString();
1414
}
1515
return s;
1616
}
Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
class Solution:
22
def digitSum(self, s: str, k: int) -> str:
3-
if len(s) <= k:
4-
return s
5-
t = []
6-
while s:
7-
t.append(str(sum(int(v) for v in s[:k])))
8-
s = s[k:]
9-
return self.digitSum(''.join(t), k)
3+
while len(s) > k:
4+
t = []
5+
n = len(s)
6+
for i in range(0, n, k):
7+
x = 0
8+
for j in range(i, min(i + k, n)):
9+
x += int(s[j])
10+
t.append(str(x))
11+
s = "".join(t)
12+
return s

‎solution/2200-2299/2244.Minimum Rounds to Complete All Tasks/README.md‎

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@
4444

4545
<!-- 这里可写通用的实现逻辑 -->
4646

47+
**方法一:哈希表**
48+
49+
我们用哈希表统计每个难度级别的任务数量,然后遍历哈希表,对于每个难度级别的任务数量,如果数量为 1ドル,ドル则无法完成所有任务,返回 $-1$;否则,计算完成该难度级别的任务需要的轮数,累加到答案中。
50+
51+
最后返回答案即可。
52+
53+
时间复杂度 $O(n),ドル空间复杂度 $O(n)$。其中 $n$ 为数组 `tasks` 的长度。
54+
4755
<!-- tabs:start -->
4856

4957
### **Python3**
@@ -54,10 +62,12 @@
5462
class Solution:
5563
def minimumRounds(self, tasks: List[int]) -> int:
5664
cnt = Counter(tasks)
57-
mi = min(cnt.values())
58-
if mi == 1:
59-
return -1
60-
return sum(v // 3 + (0 if v % 3 == 0 else 1) for v in cnt.values())
65+
ans = 0
66+
for v in cnt.values():
67+
if v == 1:
68+
return -1
69+
ans += v // 3 + (v % 3 != 0)
70+
return ans
6171
```
6272

6373
### **Java**
@@ -69,7 +79,7 @@ class Solution {
6979
public int minimumRounds(int[] tasks) {
7080
Map<Integer, Integer> cnt = new HashMap<>();
7181
for (int t : tasks) {
72-
cnt.put(t, cnt.getOrDefault(t, 0) +1);
82+
cnt.merge(t, 1, Integer::sum);
7383
}
7484
int ans = 0;
7585
for (int v : cnt.values()) {
@@ -83,37 +93,22 @@ class Solution {
8393
}
8494
```
8595

86-
### **TypeScript**
87-
88-
```ts
89-
function minimumRounds(tasks: number[]): number {
90-
let hashMap = new Map();
91-
for (let key of tasks) {
92-
hashMap.set(key, (hashMap.get(key) || 0) + 1);
93-
}
94-
let ans = 0;
95-
for (let key of hashMap.keys()) {
96-
let val = hashMap.get(key);
97-
if (val < 2) return -1;
98-
const ctn = Math.floor(val / 3) + (val % 3 == 0 ? 0 : 1);
99-
ans += ctn;
100-
}
101-
return ans;
102-
}
103-
```
104-
10596
### **C++**
10697

10798
```cpp
10899
class Solution {
109100
public:
110101
int minimumRounds(vector<int>& tasks) {
111102
unordered_map<int, int> cnt;
112-
for (int& t : tasks) ++cnt[t];
103+
for (auto& t : tasks) {
104+
++cnt[t];
105+
}
113106
int ans = 0;
114107
for (auto& [_, v] : cnt) {
115-
if (v == 1) return -1;
116-
ans += v / 3 + (v % 3 == 0 ? 0 : 1);
108+
if (v == 1) {
109+
return -1;
110+
}
111+
ans += v / 3 + (v % 3 != 0);
117112
}
118113
return ans;
119114
}
@@ -142,6 +137,25 @@ func minimumRounds(tasks []int) int {
142137
}
143138
```
144139

140+
### **TypeScript**
141+
142+
```ts
143+
function minimumRounds(tasks: number[]): number {
144+
const cnt = new Map();
145+
for (const t of tasks) {
146+
cnt.set(t, (cnt.get(t) || 0) + 1);
147+
}
148+
let ans = 0;
149+
for (const v of cnt.values()) {
150+
if (v == 1) {
151+
return -1;
152+
}
153+
ans += Math.floor(v / 3) + (v % 3 === 0 ? 0 : 1);
154+
}
155+
return ans;
156+
}
157+
```
158+
145159
### **...**
146160

147161
```

0 commit comments

Comments
(0)

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