Skip to content

Commit 46943c5

Browse files
committed
feat: add solutions to lc problems: No.1837, 1844
1 parent b6c580c commit 46943c5

File tree

8 files changed

+98
-12
lines changed

8 files changed

+98
-12
lines changed

solution/1800-1899/1837.Sum of Digits in Base K/README.md

+19-3
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,43 @@
3737
<li><code>2 <= k <= 10</code></li>
3838
</ul>
3939

40-
4140
## 解法
4241

4342
<!-- 这里可写通用的实现逻辑 -->
4443

44+
将 n 除 k 取余,直至为 0,余数相加求得结果。
45+
4546
<!-- tabs:start -->
4647

4748
### **Python3**
4849

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

5152
```python
52-
53+
class Solution:
54+
def sumBase(self, n: int, k: int) -> int:
55+
res = 0
56+
while n != 0:
57+
n, t = divmod(n, k)
58+
res += t
59+
return res
5360
```
5461

5562
### **Java**
5663

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

5966
```java
60-
67+
class Solution {
68+
public int sumBase(int n, int k) {
69+
int res = 0;
70+
while (n != 0) {
71+
res += (n % k);
72+
n /= k;
73+
}
74+
return res;
75+
}
76+
}
6177
```
6278

6379
### **...**

solution/1800-1899/1837.Sum of Digits in Base K/README_EN.md

+17-3
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,35 @@
3333
<li><code>2 &lt;= k &lt;= 10</code></li>
3434
</ul>
3535

36-
3736
## Solutions
3837

3938
<!-- tabs:start -->
4039

4140
### **Python3**
4241

4342
```python
44-
43+
class Solution:
44+
def sumBase(self, n: int, k: int) -> int:
45+
res = 0
46+
while n != 0:
47+
n, t = divmod(n, k)
48+
res += t
49+
return res
4550
```
4651

4752
### **Java**
4853

4954
```java
50-
55+
class Solution {
56+
public int sumBase(int n, int k) {
57+
int res = 0;
58+
while (n != 0) {
59+
res += (n % k);
60+
n /= k;
61+
}
62+
return res;
63+
}
64+
}
5165
```
5266

5367
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution {
2+
public int sumBase(int n, int k) {
3+
int res = 0;
4+
while (n != 0) {
5+
res += (n % k);
6+
n /= k;
7+
}
8+
return res;
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def sumBase(self, n: int, k: int) -> int:
3+
res = 0
4+
while n != 0:
5+
n, t = divmod(n, k)
6+
res += t
7+
return res

solution/1800-1899/1844.Replace All Digits with Characters/README.md

+15-3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
<li>对所有 <strong>奇数</strong> 下标处的 <code>i</code> ,满足 <code>shift(s[i-1], s[i]) &lt;= 'z'</code> 。</li>
5050
</ul>
5151

52-
5352
## 解法
5453

5554
<!-- 这里可写通用的实现逻辑 -->
@@ -61,15 +60,28 @@
6160
<!-- 这里可写当前语言的特殊实现逻辑 -->
6261

6362
```python
64-
63+
class Solution:
64+
def replaceDigits(self, s: str) -> str:
65+
s = list(s)
66+
for i in range(1, len(s), 2):
67+
s[i] = chr(ord(s[i - 1]) + int(s[i]))
68+
return ''.join(s)
6569
```
6670

6771
### **Java**
6872

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

7175
```java
72-
76+
class Solution {
77+
public String replaceDigits(String s) {
78+
char[] chars = s.toCharArray();
79+
for (int i = 1; i < chars.length; i += 2) {
80+
chars[i] = (char) (chars[i - 1] + (chars[i] - '0'));
81+
}
82+
return new String(chars);
83+
}
84+
}
7385
```
7486

7587
### **...**

solution/1800-1899/1844.Replace All Digits with Characters/README_EN.md

+15-3
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,33 @@
4747
<li><code>shift(s[i-1], s[i]) &lt;= &#39;z&#39;</code> for all <strong>odd</strong> indices <code>i</code>.</li>
4848
</ul>
4949

50-
5150
## Solutions
5251

5352
<!-- tabs:start -->
5453

5554
### **Python3**
5655

5756
```python
58-
57+
class Solution:
58+
def replaceDigits(self, s: str) -> str:
59+
s = list(s)
60+
for i in range(1, len(s), 2):
61+
s[i] = chr(ord(s[i - 1]) + int(s[i]))
62+
return ''.join(s)
5963
```
6064

6165
### **Java**
6266

6367
```java
64-
68+
class Solution {
69+
public String replaceDigits(String s) {
70+
char[] chars = s.toCharArray();
71+
for (int i = 1; i < chars.length; i += 2) {
72+
chars[i] = (char) (chars[i - 1] + (chars[i] - '0'));
73+
}
74+
return new String(chars);
75+
}
76+
}
6577
```
6678

6779
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution {
2+
public String replaceDigits(String s) {
3+
char[] chars = s.toCharArray();
4+
for (int i = 1; i < chars.length; i += 2) {
5+
chars[i] = (char) (chars[i - 1] + (chars[i] - '0'));
6+
}
7+
return new String(chars);
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution:
2+
def replaceDigits(self, s: str) -> str:
3+
s = list(s)
4+
for i in range(1, len(s), 2):
5+
s[i] = chr(ord(s[i - 1]) + int(s[i]))
6+
return ''.join(s)

0 commit comments

Comments
 (0)