Skip to content

Commit 2365750

Browse files
committed
feat: add ts solution to lc problem: No.0038
No.0038.Count and Say
1 parent 583bcfb commit 2365750

File tree

3 files changed

+201
-0
lines changed

3 files changed

+201
-0
lines changed

solution/0000-0099/0038.Count and Say/README.md

+91
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,97 @@ func countAndSay(n int) string {
171171
}
172172
```
173173

174+
### **C#**
175+
176+
```cs
177+
using System.Text;
178+
public class Solution {
179+
public string CountAndSay(int n) {
180+
var s = "1";
181+
while (n > 1)
182+
{
183+
var sb = new StringBuilder();
184+
var lastChar = '1';
185+
var count = 0;
186+
foreach (var ch in s)
187+
{
188+
if (count > 0 && lastChar == ch)
189+
{
190+
++count;
191+
}
192+
else
193+
{
194+
if (count > 0)
195+
{
196+
sb.Append(count);
197+
sb.Append(lastChar);
198+
}
199+
lastChar = ch;
200+
count = 1;
201+
}
202+
}
203+
if (count > 0)
204+
{
205+
sb.Append(count);
206+
sb.Append(lastChar);
207+
}
208+
s = sb.ToString();
209+
--n;
210+
}
211+
return s;
212+
}
213+
}
214+
```
215+
216+
### **JavaScript**
217+
218+
```js
219+
const countAndSay = function (n) {
220+
let s = '1';
221+
222+
for (let i = 2; i <= n; i++) {
223+
let count = 1,
224+
str = '',
225+
len = s.length;
226+
227+
for (let j = 0; j < len; j++) {
228+
if (j < len - 1 && s[j] === s[j + 1]) {
229+
count++;
230+
} else {
231+
str += `${count}${s[j]}`;
232+
count = 1;
233+
}
234+
}
235+
s = str;
236+
}
237+
return s;
238+
};
239+
```
240+
241+
### **TypeScript**
242+
243+
```ts
244+
function countAndSay(n: number): string {
245+
let s = '1';
246+
for (let i = 1; i < n; i++) {
247+
let t = '';
248+
let cur = s[0];
249+
let count = 1;
250+
for (let j = 1; j < s.length; j++) {
251+
if (s[j] !== cur) {
252+
t += `${count}${cur}`;
253+
cur = s[j];
254+
count = 0;
255+
}
256+
count++;
257+
}
258+
t += `${count}${cur}`;
259+
s = t;
260+
}
261+
return s;
262+
}
263+
```
264+
174265
### **...**
175266

176267
```

solution/0000-0099/0038.Count and Say/README_EN.md

+91
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,97 @@ func countAndSay(n int) string {
141141
}
142142
```
143143

144+
### **C#**
145+
146+
```cs
147+
using System.Text;
148+
public class Solution {
149+
public string CountAndSay(int n) {
150+
var s = "1";
151+
while (n > 1)
152+
{
153+
var sb = new StringBuilder();
154+
var lastChar = '1';
155+
var count = 0;
156+
foreach (var ch in s)
157+
{
158+
if (count > 0 && lastChar == ch)
159+
{
160+
++count;
161+
}
162+
else
163+
{
164+
if (count > 0)
165+
{
166+
sb.Append(count);
167+
sb.Append(lastChar);
168+
}
169+
lastChar = ch;
170+
count = 1;
171+
}
172+
}
173+
if (count > 0)
174+
{
175+
sb.Append(count);
176+
sb.Append(lastChar);
177+
}
178+
s = sb.ToString();
179+
--n;
180+
}
181+
return s;
182+
}
183+
}
184+
```
185+
186+
### **JavaScript**
187+
188+
```js
189+
const countAndSay = function (n) {
190+
let s = '1';
191+
192+
for (let i = 2; i <= n; i++) {
193+
let count = 1,
194+
str = '',
195+
len = s.length;
196+
197+
for (let j = 0; j < len; j++) {
198+
if (j < len - 1 && s[j] === s[j + 1]) {
199+
count++;
200+
} else {
201+
str += `${count}${s[j]}`;
202+
count = 1;
203+
}
204+
}
205+
s = str;
206+
}
207+
return s;
208+
};
209+
```
210+
211+
### **TypeScript**
212+
213+
```ts
214+
function countAndSay(n: number): string {
215+
let s = '1';
216+
for (let i = 1; i < n; i++) {
217+
let t = '';
218+
let cur = s[0];
219+
let count = 1;
220+
for (let j = 1; j < s.length; j++) {
221+
if (s[j] !== cur) {
222+
t += `${count}${cur}`;
223+
cur = s[j];
224+
count = 0;
225+
}
226+
count++;
227+
}
228+
t += `${count}${cur}`;
229+
s = t;
230+
}
231+
return s;
232+
}
233+
```
234+
144235
### **...**
145236

146237
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function countAndSay(n: number): string {
2+
let s = '1';
3+
for (let i = 1; i < n; i++) {
4+
let t = '';
5+
let cur = s[0];
6+
let count = 1;
7+
for (let j = 1; j < s.length; j++) {
8+
if (s[j] !== cur) {
9+
t += `${count}${cur}`;
10+
cur = s[j];
11+
count = 0;
12+
}
13+
count++;
14+
}
15+
t += `${count}${cur}`;
16+
s = t;
17+
}
18+
return s;
19+
}

0 commit comments

Comments
 (0)