File tree 8 files changed +98
-12
lines changed
1837.Sum of Digits in Base K
1844.Replace All Digits with Characters
8 files changed +98
-12
lines changed Original file line number Diff line number Diff line change 37
37
<li><code>2 <= k <= 10</code></li>
38
38
</ul >
39
39
40
-
41
40
## 解法
42
41
43
42
<!-- 这里可写通用的实现逻辑 -->
44
43
44
+ 将 n 除 k 取余,直至为 0,余数相加求得结果。
45
+
45
46
<!-- tabs:start -->
46
47
47
48
### ** Python3**
48
49
49
50
<!-- 这里可写当前语言的特殊实现逻辑 -->
50
51
51
52
``` 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
53
60
```
54
61
55
62
### ** Java**
56
63
57
64
<!-- 这里可写当前语言的特殊实现逻辑 -->
58
65
59
66
``` 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
+ }
61
77
```
62
78
63
79
### ** ...**
Original file line number Diff line number Diff line change 33
33
<li><code>2 <= k <= 10</code></li>
34
34
</ul >
35
35
36
-
37
36
## Solutions
38
37
39
38
<!-- tabs:start -->
40
39
41
40
### ** Python3**
42
41
43
42
``` 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
45
50
```
46
51
47
52
### ** Java**
48
53
49
54
``` 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
+ }
51
65
```
52
66
53
67
### ** ...**
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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
Original file line number Diff line number Diff line change 49
49
<li>对所有 <strong>奇数</strong> 下标处的 <code>i</code> ,满足 <code>shift(s[i-1], s[i]) <= 'z'</code> 。</li>
50
50
</ul >
51
51
52
-
53
52
## 解法
54
53
55
54
<!-- 这里可写通用的实现逻辑 -->
61
60
<!-- 这里可写当前语言的特殊实现逻辑 -->
62
61
63
62
``` 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)
65
69
```
66
70
67
71
### ** Java**
68
72
69
73
<!-- 这里可写当前语言的特殊实现逻辑 -->
70
74
71
75
``` 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
+ }
73
85
```
74
86
75
87
### ** ...**
Original file line number Diff line number Diff line change 47
47
<li><code>shift(s[i-1], s[i]) <= 'z'</code> for all <strong>odd</strong> indices <code>i</code>.</li>
48
48
</ul >
49
49
50
-
51
50
## Solutions
52
51
53
52
<!-- tabs:start -->
54
53
55
54
### ** Python3**
56
55
57
56
``` 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)
59
63
```
60
64
61
65
### ** Java**
62
66
63
67
``` 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
+ }
65
77
```
66
78
67
79
### ** ...**
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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 )
You can’t perform that action at this time.
0 commit comments