Skip to content

Commit 93b5622

Browse files
committed
feat: add solutions to lc problems: No.1796, 2114
- No.1796.Second Largest Digit in a String - No.2114.Maximum Number of Words Found in Sentences
1 parent a3c9b37 commit 93b5622

File tree

10 files changed

+318
-2
lines changed

10 files changed

+318
-2
lines changed

solution/1700-1799/1796.Second Largest Digit in a String/README.md

+64
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,70 @@ func secondHighest(s string) int {
212212
}
213213
```
214214

215+
### **TypeScript**
216+
217+
```ts
218+
function secondHighest(s: string): number {
219+
let first = -1;
220+
let second = -1;
221+
for (const c of s) {
222+
if (c >= '0' && c <= '9') {
223+
const num = c.charCodeAt(0) - '0'.charCodeAt(0);
224+
if (first < num) {
225+
[first, second] = [num, first];
226+
} else if (first !== num && second < num) {
227+
second = num;
228+
}
229+
}
230+
}
231+
return second;
232+
}
233+
```
234+
235+
### **Rust**
236+
237+
```rust
238+
impl Solution {
239+
pub fn second_highest(s: String) -> i32 {
240+
let mut first = -1;
241+
let mut second = -1;
242+
for c in s.as_bytes() {
243+
if char::is_digit(*c as char, 10) {
244+
let num = (c - b'0') as i32;
245+
if first < num {
246+
second = first;
247+
first = num;
248+
} else if num < first && second < num {
249+
second = num;
250+
}
251+
}
252+
}
253+
second
254+
}
255+
}
256+
```
257+
258+
### **C**
259+
260+
```c
261+
int secondHighest(char *s) {
262+
int first = -1;
263+
int second = -1;
264+
for (int i = 0; s[i]; i++) {
265+
if (isdigit(s[i])) {
266+
int num = s[i] - '0';
267+
if (num > first) {
268+
second = first;
269+
first = num;
270+
} else if (num < first && second < num) {
271+
second = num;
272+
}
273+
}
274+
}
275+
return second;
276+
}
277+
```
278+
215279
### **...**
216280
217281
```

solution/1700-1799/1796.Second Largest Digit in a String/README_EN.md

+64
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,70 @@ func secondHighest(s string) int {
170170
}
171171
```
172172

173+
### **TypeScript**
174+
175+
```ts
176+
function secondHighest(s: string): number {
177+
let first = -1;
178+
let second = -1;
179+
for (const c of s) {
180+
if (c >= '0' && c <= '9') {
181+
const num = c.charCodeAt(0) - '0'.charCodeAt(0);
182+
if (first < num) {
183+
[first, second] = [num, first];
184+
} else if (first !== num && second < num) {
185+
second = num;
186+
}
187+
}
188+
}
189+
return second;
190+
}
191+
```
192+
193+
### **Rust**
194+
195+
```rust
196+
impl Solution {
197+
pub fn second_highest(s: String) -> i32 {
198+
let mut first = -1;
199+
let mut second = -1;
200+
for c in s.as_bytes() {
201+
if char::is_digit(*c as char, 10) {
202+
let num = (c - b'0') as i32;
203+
if first < num {
204+
second = first;
205+
first = num;
206+
} else if num < first && second < num {
207+
second = num;
208+
}
209+
}
210+
}
211+
second
212+
}
213+
}
214+
```
215+
216+
### **C**
217+
218+
```c
219+
int secondHighest(char *s) {
220+
int first = -1;
221+
int second = -1;
222+
for (int i = 0; s[i]; i++) {
223+
if (isdigit(s[i])) {
224+
int num = s[i] - '0';
225+
if (num > first) {
226+
second = first;
227+
first = num;
228+
} else if (num < first && second < num) {
229+
second = num;
230+
}
231+
}
232+
}
233+
return second;
234+
}
235+
```
236+
173237
### **...**
174238
175239
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
int secondHighest(char *s) {
2+
int first = -1;
3+
int second = -1;
4+
for (int i = 0; s[i]; i++) {
5+
if (isdigit(s[i])) {
6+
int num = s[i] - '0';
7+
if (num > first) {
8+
second = first;
9+
first = num;
10+
} else if (num < first && second < num) {
11+
second = num;
12+
}
13+
}
14+
}
15+
return second;
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
impl Solution {
2+
pub fn second_highest(s: String) -> i32 {
3+
let mut first = -1;
4+
let mut second = -1;
5+
for c in s.as_bytes() {
6+
if char::is_digit(*c as char, 10) {
7+
let num = (c - b'0') as i32;
8+
if first < num {
9+
second = first;
10+
first = num;
11+
} else if num < first && second < num {
12+
second = num;
13+
}
14+
}
15+
}
16+
second
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function secondHighest(s: string): number {
2+
let first = -1;
3+
let second = -1;
4+
for (const c of s) {
5+
if (c >= '0' && c <= '9') {
6+
const num = c.charCodeAt(0) - '0'.charCodeAt(0);
7+
if (first < num) {
8+
[first, second] = [num, first];
9+
} else if (first !== num && second < num) {
10+
second = num;
11+
}
12+
}
13+
}
14+
return second;
15+
}

solution/2100-2199/2114.Maximum Number of Words Found in Sentences/README.md

+50-2
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,58 @@ func max(a, b int) int {
142142

143143
### **TypeScript**
144144

145-
<!-- 这里可写当前语言的特殊实现逻辑 -->
146-
147145
```ts
146+
function mostWordsFound(sentences: string[]): number {
147+
return sentences.reduce(
148+
(r, s) =>
149+
Math.max(
150+
r,
151+
[...s].reduce((r, c) => r + (c === ' ' ? 1 : 0), 1),
152+
),
153+
0,
154+
);
155+
}
156+
```
157+
158+
### **Rust**
159+
160+
```rust
161+
impl Solution {
162+
pub fn most_words_found(sentences: Vec<String>) -> i32 {
163+
let mut ans = 0;
164+
for s in sentences.iter() {
165+
let mut count = 1;
166+
for c in s.as_bytes() {
167+
if *c == b' ' {
168+
count += 1;
169+
}
170+
}
171+
ans = ans.max(count);
172+
}
173+
ans
174+
}
175+
}
176+
```
177+
178+
### **C**
179+
180+
```c
181+
#define max(a, b) (((a) > (b)) ? (a) : (b))
148182

183+
int mostWordsFound(char **sentences, int sentencesSize) {
184+
int ans = 0;
185+
for (int i = 0; i < sentencesSize; i++) {
186+
char *s = sentences[i];
187+
int count = 1;
188+
for (int j = 0; s[j]; j++) {
189+
if (s[j] == ' ') {
190+
count++;
191+
}
192+
}
193+
ans = max(ans, count);
194+
}
195+
return ans;
196+
}
149197
```
150198
151199
### **...**

solution/2100-2199/2114.Maximum Number of Words Found in Sentences/README_EN.md

+50
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,57 @@ func max(a, b int) int {
133133
### **TypeScript**
134134

135135
```ts
136+
function mostWordsFound(sentences: string[]): number {
137+
return sentences.reduce(
138+
(r, s) =>
139+
Math.max(
140+
r,
141+
[...s].reduce((r, c) => r + (c === ' ' ? 1 : 0), 1),
142+
),
143+
0,
144+
);
145+
}
146+
```
147+
148+
### **Rust**
149+
150+
```rust
151+
impl Solution {
152+
pub fn most_words_found(sentences: Vec<String>) -> i32 {
153+
let mut ans = 0;
154+
for s in sentences.iter() {
155+
let mut count = 1;
156+
for c in s.as_bytes() {
157+
if *c == b' ' {
158+
count += 1;
159+
}
160+
}
161+
ans = ans.max(count);
162+
}
163+
ans
164+
}
165+
}
166+
```
167+
168+
### **C**
136169

170+
```c
171+
#define max(a, b) (((a) > (b)) ? (a) : (b))
172+
173+
int mostWordsFound(char **sentences, int sentencesSize) {
174+
int ans = 0;
175+
for (int i = 0; i < sentencesSize; i++) {
176+
char *s = sentences[i];
177+
int count = 1;
178+
for (int j = 0; s[j]; j++) {
179+
if (s[j] == ' ') {
180+
count++;
181+
}
182+
}
183+
ans = max(ans, count);
184+
}
185+
return ans;
186+
}
137187
```
138188
139189
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#define max(a, b) (((a) > (b)) ? (a) : (b))
2+
3+
int mostWordsFound(char **sentences, int sentencesSize) {
4+
int ans = 0;
5+
for (int i = 0; i < sentencesSize; i++) {
6+
char *s = sentences[i];
7+
int count = 1;
8+
for (int j = 0; s[j]; j++) {
9+
if (s[j] == ' ') {
10+
count++;
11+
}
12+
}
13+
ans = max(ans, count);
14+
}
15+
return ans;
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
impl Solution {
2+
pub fn most_words_found(sentences: Vec<String>) -> i32 {
3+
let mut ans = 0;
4+
for s in sentences.iter() {
5+
let mut count = 1;
6+
for c in s.as_bytes() {
7+
if *c == b' ' {
8+
count += 1;
9+
}
10+
}
11+
ans = ans.max(count);
12+
}
13+
ans
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function mostWordsFound(sentences: string[]): number {
2+
return sentences.reduce(
3+
(r, s) =>
4+
Math.max(
5+
r,
6+
[...s].reduce((r, c) => r + (c === ' ' ? 1 : 0), 1),
7+
),
8+
0,
9+
);
10+
}

0 commit comments

Comments
 (0)