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 efbce23

Browse files
committed
feat: add solutions to lc problem: No.1663
No.1663.Smallest String With A Given Numeric Value
1 parent 9ccb348 commit efbce23

File tree

9 files changed

+189
-6
lines changed

9 files changed

+189
-6
lines changed

‎solution/1600-1699/1662.Check If Two String Arrays are Equivalent/README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ word2 表示的字符串为 "a" + "bc" -> "abc"
6161

6262
方法一中,我们是将两个数组中的字符串拼接成两个新的字符串,有额外的空间开销。我们也可以直接遍历两个数组,逐个字符比较。
6363

64-
我们使用两个指针 $i$ 和 $j$ 分别指向两个字符串数组,用另外两个指针 $x$ 和 $y$ 分别指向字符串对应的字符。初始时,$i = j = x = y = 0$。
64+
我们使用两个指针 $i$ 和 $j$ 分别指向两个字符串数组,用另外两个指针 $x$ 和 $y$ 分别指向字符串对应的字符。初始时$i = j = x = y = 0$。
6565

66-
每次比较 $word1[i][x]$ 和 $word2[j][y],ドル如果不相等,直接返回 `false`。否则,将 $x$ 和 $y$ 分别加一,如果 $x$ 或 $y$ 超出了对应的字符串的长度,将对应的字符串指针 $i$ 或 $j$ 加一,然后将 $x$ 和 $y$ 重置为 0
66+
每次比较 $word1[i][x]$ 和 $word2[j][y],ドル如果不相等,直接返回 `false`。否则,将 $x$ 和 $y$ 分别加 1ドル$,如果 $x$ 或 $y$ 超出了对应的字符串的长度,将对应的字符串指针 $i$ 或 $j$ 加 1ドル$,然后将 $x$ 和 $y$ 重置为 0ドル$
6767

6868
如果两个字符串数组遍历完毕,返回 `true`,否则返回 `false`
6969

‎solution/1600-1699/1663.Smallest String With A Given Numeric Value/README.md‎

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,89 @@
4848

4949
<!-- 这里可写通用的实现逻辑 -->
5050

51+
**方法一:贪心**
52+
53+
我们先将字符串的每个字符都初始化为 `'a'`,此时剩余的数值为 $k-n$。从后往前遍历字符串,每次贪心将当前位置的字符替换为能够使得剩余的数字最小的字符,直到剩余的数字不超过 25ドル$。最后将剩余的数字加到我们遍历到的位置上即可。
54+
55+
时间复杂度 $O(n),ドル忽略答案的空间消耗,空间复杂度 $O(1)$。其中 $n$ 为字符串的长度。
56+
5157
<!-- tabs:start -->
5258

5359
### **Python3**
5460

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

5763
```python
58-
64+
class Solution:
65+
def getSmallestString(self, n: int, k: int) -> str:
66+
ans = ['a'] * n
67+
i, d = n - 1, k - n
68+
while d > 25:
69+
ans[i] = 'z'
70+
d -= 25
71+
i -= 1
72+
ans[i] = chr(ord(ans[i]) + d)
73+
return ''.join(ans)
5974
```
6075

6176
### **Java**
6277

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

6580
```java
81+
class Solution {
82+
public String getSmallestString(int n, int k) {
83+
char[] ans = new char[n];
84+
Arrays.fill(ans, 'a');
85+
int i = n - 1;
86+
int d = k - n;
87+
while (d > 25) {
88+
ans[i--] = 'z';
89+
d -= 25;
90+
}
91+
ans[i] = (char) ('a' + d);
92+
return String.valueOf(ans);
93+
}
94+
}
95+
```
96+
97+
### **C++**
98+
99+
```cpp
100+
class Solution {
101+
public:
102+
string getSmallestString(int n, int k) {
103+
string ans(n, 'a');
104+
int i = n - 1;
105+
int d = k - n;
106+
while (d > 25) {
107+
ans[i--] += 25;
108+
d -= 25;
109+
}
110+
ans[i] += d;
111+
return ans;
112+
}
113+
};
114+
```
66115
116+
### **Go**
117+
118+
```go
119+
func getSmallestString(n int, k int) string {
120+
ans := make([]byte, n)
121+
for i := range ans {
122+
ans[i] = 'a'
123+
}
124+
i := n - 1
125+
d := k - n
126+
for d > 25 {
127+
ans[i] = 'z'
128+
i--
129+
d -= 25
130+
}
131+
ans[i] += byte(d)
132+
return string(ans)
133+
}
67134
```
68135

69136
### **...**

‎solution/1600-1699/1663.Smallest String With A Given Numeric Value/README_EN.md‎

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,74 @@
4343
### **Python3**
4444

4545
```python
46-
46+
class Solution:
47+
def getSmallestString(self, n: int, k: int) -> str:
48+
ans = ['a'] * n
49+
i, d = n - 1, k - n
50+
while d > 25:
51+
ans[i] = 'z'
52+
d -= 25
53+
i -= 1
54+
ans[i] = chr(ord(ans[i]) + d)
55+
return ''.join(ans)
4756
```
4857

4958
### **Java**
5059

5160
```java
61+
class Solution {
62+
public String getSmallestString(int n, int k) {
63+
char[] ans = new char[n];
64+
Arrays.fill(ans, 'a');
65+
int i = n - 1;
66+
int d = k - n;
67+
while (d > 25) {
68+
ans[i--] = 'z';
69+
d -= 25;
70+
}
71+
ans[i] = (char) ('a' + d);
72+
return String.valueOf(ans);
73+
}
74+
}
75+
```
76+
77+
### **C++**
78+
79+
```cpp
80+
class Solution {
81+
public:
82+
string getSmallestString(int n, int k) {
83+
string ans(n, 'a');
84+
int i = n - 1;
85+
int d = k - n;
86+
while (d > 25) {
87+
ans[i--] += 25;
88+
d -= 25;
89+
}
90+
ans[i] += d;
91+
return ans;
92+
}
93+
};
94+
```
5295
96+
### **Go**
97+
98+
```go
99+
func getSmallestString(n int, k int) string {
100+
ans := make([]byte, n)
101+
for i := range ans {
102+
ans[i] = 'a'
103+
}
104+
i := n - 1
105+
d := k - n
106+
for d > 25 {
107+
ans[i] = 'z'
108+
i--
109+
d -= 25
110+
}
111+
ans[i] += byte(d)
112+
return string(ans)
113+
}
53114
```
54115

55116
### **...**
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
string getSmallestString(int n, int k) {
4+
string ans(n, 'a');
5+
int i = n - 1;
6+
int d = k - n;
7+
while (d > 25) {
8+
ans[i--] += 25;
9+
d -= 25;
10+
}
11+
ans[i] += d;
12+
return ans;
13+
}
14+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
func getSmallestString(n int, k int) string {
2+
ans := make([]byte, n)
3+
for i := range ans {
4+
ans[i] = 'a'
5+
}
6+
i := n - 1
7+
d := k - n
8+
for d > 25 {
9+
ans[i] = 'z'
10+
i--
11+
d -= 25
12+
}
13+
ans[i] += byte(d)
14+
return string(ans)
15+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public String getSmallestString(int n, int k) {
3+
char[] ans = new char[n];
4+
Arrays.fill(ans, 'a');
5+
int i = n - 1;
6+
int d = k - n;
7+
while (d > 25) {
8+
ans[i--] = 'z';
9+
d -= 25;
10+
}
11+
ans[i] = (char) ('a' + d);
12+
return String.valueOf(ans);
13+
}
14+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def getSmallestString(self, n: int, k: int) -> str:
3+
ans = ['a'] * n
4+
i, d = n - 1, k - n
5+
while d > 25:
6+
ans[i] = 'z'
7+
d -= 25
8+
i -= 1
9+
ans[i] = chr(ord(ans[i]) + d)
10+
return ''.join(ans)

‎solution/2400-2499/2455.Average Value of Even Numbers That Are Divisible by Three/Solution.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ def averageValue(self, nums: List[int]) -> int:
55
if v % 6 == 0:
66
s += v
77
n += 1
8-
return 0 if n == 0 else s // n
8+
return 0 if n == 0 else s // n

‎solution/2400-2499/2456.Most Popular Video Creator/Solution.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
class Solution:
2-
def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:
2+
def mostPopularCreator(
3+
self, creators: List[str], ids: List[str], views: List[int]
4+
) -> List[List[str]]:
35
cnt = defaultdict(int)
46
d = {}
57
x = {}

0 commit comments

Comments
(0)

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