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 3a39203

Browse files
committed
feat: add solutions to lc problem: No.1370
No.1370.Increasing Decreasing String
1 parent dbb90fc commit 3a39203

File tree

6 files changed

+288
-4
lines changed

6 files changed

+288
-4
lines changed

‎solution/1300-1399/1370.Increasing Decreasing String/README.md‎

Lines changed: 99 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@
6969
<li><code>s</code>&nbsp;只包含小写英文字母。</li>
7070
</ul>
7171

72-
7372
## 解法
7473

7574
<!-- 这里可写通用的实现逻辑 -->
@@ -81,15 +80,113 @@
8180
<!-- 这里可写当前语言的特殊实现逻辑 -->
8281

8382
```python
84-
83+
class Solution:
84+
def sortString(self, s: str) -> str:
85+
counter = [0] * 26
86+
for c in s:
87+
counter[ord(c) - ord('a')] += 1
88+
ans = []
89+
while len(ans) < len(s):
90+
for i in range(26):
91+
if counter[i]:
92+
ans.append(chr(i + ord('a')))
93+
counter[i] -= 1
94+
for i in range(25, -1, -1):
95+
if counter[i]:
96+
ans.append(chr(i + ord('a')))
97+
counter[i] -= 1
98+
return ''.join(ans)
8599
```
86100

87101
### **Java**
88102

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

91105
```java
106+
class Solution {
107+
public String sortString(String s) {
108+
int[] counter = new int[26];
109+
for (char c : s.toCharArray()) {
110+
++counter[c - 'a'];
111+
}
112+
StringBuilder sb = new StringBuilder();
113+
while (sb.length() < s.length()) {
114+
for (int i = 0; i < 26; ++i) {
115+
if (counter[i] > 0) {
116+
sb.append((char) ('a' + i));
117+
--counter[i];
118+
}
119+
}
120+
for (int i = 25; i >= 0; --i) {
121+
if (counter[i] > 0) {
122+
sb.append((char) ('a' + i));
123+
--counter[i];
124+
}
125+
}
126+
}
127+
return sb.toString();
128+
}
129+
}
130+
```
131+
132+
### **C++**
133+
134+
```cpp
135+
class Solution {
136+
public:
137+
string sortString(string s) {
138+
vector<int> counter(26);
139+
for (char c : s) ++counter[c - 'a'];
140+
string ans = "";
141+
while (ans.size() < s.size())
142+
{
143+
for (int i = 0; i < 26; ++i)
144+
{
145+
if (counter[i])
146+
{
147+
ans += (i + 'a');
148+
--counter[i];
149+
}
150+
}
151+
for (int i = 25; i >= 0; --i)
152+
{
153+
if (counter[i])
154+
{
155+
ans += (i + 'a');
156+
--counter[i];
157+
}
158+
}
159+
}
160+
return ans;
161+
}
162+
};
163+
```
92164
165+
### **Go**
166+
167+
```go
168+
func sortString(s string) string {
169+
counter := ['z' + 1]int{}
170+
for _, c := range s {
171+
counter[c]++
172+
}
173+
var ans []byte
174+
for len(ans) < len(s) {
175+
for i := byte('a'); i <= 'z'; i++ {
176+
if counter[i] > 0 {
177+
ans = append(ans, i)
178+
counter[i]--
179+
}
180+
}
181+
for i := byte('z'); i >= 'a'; i-- {
182+
if counter[i] > 0 {
183+
ans = append(ans, i)
184+
counter[i]--
185+
}
186+
}
187+
}
188+
return string(ans)
189+
}
93190
```
94191

95192
### **...**

‎solution/1300-1399/1370.Increasing Decreasing String/README_EN.md‎

Lines changed: 99 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,21 +70,118 @@ After steps 4, 5 and 6 of the second iteration, result = &quot;abccbaabccba&quot
7070
<li><code>s</code> contains only lower-case English letters.</li>
7171
</ul>
7272

73-
7473
## Solutions
7574

7675
<!-- tabs:start -->
7776

7877
### **Python3**
7978

8079
```python
81-
80+
class Solution:
81+
def sortString(self, s: str) -> str:
82+
counter = [0] * 26
83+
for c in s:
84+
counter[ord(c) - ord('a')] += 1
85+
ans = []
86+
while len(ans) < len(s):
87+
for i in range(26):
88+
if counter[i]:
89+
ans.append(chr(i + ord('a')))
90+
counter[i] -= 1
91+
for i in range(25, -1, -1):
92+
if counter[i]:
93+
ans.append(chr(i + ord('a')))
94+
counter[i] -= 1
95+
return ''.join(ans)
8296
```
8397

8498
### **Java**
8599

86100
```java
101+
class Solution {
102+
public String sortString(String s) {
103+
int[] counter = new int[26];
104+
for (char c : s.toCharArray()) {
105+
++counter[c - 'a'];
106+
}
107+
StringBuilder sb = new StringBuilder();
108+
while (sb.length() < s.length()) {
109+
for (int i = 0; i < 26; ++i) {
110+
if (counter[i] > 0) {
111+
sb.append((char) ('a' + i));
112+
--counter[i];
113+
}
114+
}
115+
for (int i = 25; i >= 0; --i) {
116+
if (counter[i] > 0) {
117+
sb.append((char) ('a' + i));
118+
--counter[i];
119+
}
120+
}
121+
}
122+
return sb.toString();
123+
}
124+
}
125+
```
126+
127+
### **C++**
128+
129+
```cpp
130+
class Solution {
131+
public:
132+
string sortString(string s) {
133+
vector<int> counter(26);
134+
for (char c : s) ++counter[c - 'a'];
135+
string ans = "";
136+
while (ans.size() < s.size())
137+
{
138+
for (int i = 0; i < 26; ++i)
139+
{
140+
if (counter[i])
141+
{
142+
ans += (i + 'a');
143+
--counter[i];
144+
}
145+
}
146+
for (int i = 25; i >= 0; --i)
147+
{
148+
if (counter[i])
149+
{
150+
ans += (i + 'a');
151+
--counter[i];
152+
}
153+
}
154+
}
155+
return ans;
156+
}
157+
};
158+
```
87159
160+
### **Go**
161+
162+
```go
163+
func sortString(s string) string {
164+
counter := ['z' + 1]int{}
165+
for _, c := range s {
166+
counter[c]++
167+
}
168+
var ans []byte
169+
for len(ans) < len(s) {
170+
for i := byte('a'); i <= 'z'; i++ {
171+
if counter[i] > 0 {
172+
ans = append(ans, i)
173+
counter[i]--
174+
}
175+
}
176+
for i := byte('z'); i >= 'a'; i-- {
177+
if counter[i] > 0 {
178+
ans = append(ans, i)
179+
counter[i]--
180+
}
181+
}
182+
}
183+
return string(ans)
184+
}
88185
```
89186

90187
### **...**
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public:
3+
string sortString(string s) {
4+
vector<int> counter(26);
5+
for (char c : s) ++counter[c - 'a'];
6+
string ans = "";
7+
while (ans.size() < s.size())
8+
{
9+
for (int i = 0; i < 26; ++i)
10+
{
11+
if (counter[i])
12+
{
13+
ans += (i + 'a');
14+
--counter[i];
15+
}
16+
}
17+
for (int i = 25; i >= 0; --i)
18+
{
19+
if (counter[i])
20+
{
21+
ans += (i + 'a');
22+
--counter[i];
23+
}
24+
}
25+
}
26+
return ans;
27+
}
28+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
func sortString(s string) string {
2+
counter := ['z' + 1]int{}
3+
for _, c := range s {
4+
counter[c]++
5+
}
6+
var ans []byte
7+
for len(ans) < len(s) {
8+
for i := byte('a'); i <= 'z'; i++ {
9+
if counter[i] > 0 {
10+
ans = append(ans, i)
11+
counter[i]--
12+
}
13+
}
14+
for i := byte('z'); i >= 'a'; i-- {
15+
if counter[i] > 0 {
16+
ans = append(ans, i)
17+
counter[i]--
18+
}
19+
}
20+
}
21+
return string(ans)
22+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public String sortString(String s) {
3+
int[] counter = new int[26];
4+
for (char c : s.toCharArray()) {
5+
++counter[c - 'a'];
6+
}
7+
StringBuilder sb = new StringBuilder();
8+
while (sb.length() < s.length()) {
9+
for (int i = 0; i < 26; ++i) {
10+
if (counter[i] > 0) {
11+
sb.append((char) ('a' + i));
12+
--counter[i];
13+
}
14+
}
15+
for (int i = 25; i >= 0; --i) {
16+
if (counter[i] > 0) {
17+
sb.append((char) ('a' + i));
18+
--counter[i];
19+
}
20+
}
21+
}
22+
return sb.toString();
23+
}
24+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def sortString(self, s: str) -> str:
3+
counter = [0] * 26
4+
for c in s:
5+
counter[ord(c) - ord('a')] += 1
6+
ans = []
7+
while len(ans) < len(s):
8+
for i in range(26):
9+
if counter[i]:
10+
ans.append(chr(i + ord('a')))
11+
counter[i] -= 1
12+
for i in range(25, -1, -1):
13+
if counter[i]:
14+
ans.append(chr(i + ord('a')))
15+
counter[i] -= 1
16+
return ''.join(ans)

0 commit comments

Comments
(0)

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