Skip to content

Commit f592a6d

Browse files
committed
feat: add solutions to lc problems: No.2413~2416
- No.2413.Smallest Even Multiple - No.2414.Length of the Longest Alphabetical Continuous Substring - No.2415.Reverse Odd Levels of Binary Tree - No.2416.Sum of Prefix Scores of Strings
1 parent f0ed555 commit f592a6d

File tree

17 files changed

+564
-6
lines changed

17 files changed

+564
-6
lines changed

solution/2400-2499/2413.Smallest Even Multiple/README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,39 @@ func smallestEvenMultiple(n int) int {
8888
}
8989
```
9090

91+
### **C**
92+
93+
```c
94+
int smallestEvenMultiple(int n) {
95+
return n % 2 == 0 ? n : n * 2;
96+
}
97+
```
98+
9199
### **TypeScript**
92100
93101
```ts
102+
function smallestEvenMultiple(n: number): number {
103+
return n % 2 === 0 ? n : n * 2;
104+
}
105+
```
106+
107+
### **Rust**
94108

109+
```rust
110+
impl Solution {
111+
pub fn smallest_even_multiple(n: i32) -> i32 {
112+
if n % 2 == 0 {
113+
return n;
114+
}
115+
n * 2
116+
}
117+
}
95118
```
96119

97120
### **...**
98121

99122
```
100123
101-
102124
```
103125

104126
<!-- tabs:end -->

solution/2400-2499/2413.Smallest Even Multiple/README_EN.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,39 @@ func smallestEvenMultiple(n int) int {
7474
}
7575
```
7676

77+
### **C**
78+
79+
```c
80+
int smallestEvenMultiple(int n) {
81+
return n % 2 == 0 ? n : n * 2;
82+
}
83+
```
84+
7785
### **TypeScript**
7886
7987
```ts
88+
function smallestEvenMultiple(n: number): number {
89+
return n % 2 === 0 ? n : n * 2;
90+
}
91+
```
92+
93+
### **Rust**
8094

95+
```rust
96+
impl Solution {
97+
pub fn smallest_even_multiple(n: i32) -> i32 {
98+
if n % 2 == 0 {
99+
return n;
100+
}
101+
n * 2
102+
}
103+
}
81104
```
82105

83106
### **...**
84107

85108
```
86109
87-
88110
```
89111

90112
<!-- tabs:end -->
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
int smallestEvenMultiple(int n) {
2+
return n % 2 == 0 ? n : n * 2;
3+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
impl Solution {
2+
pub fn smallest_even_multiple(n: i32) -> i32 {
3+
if n % 2 == 0 {
4+
return n;
5+
}
6+
n * 2
7+
}
8+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function smallestEvenMultiple(n: number): number {
2+
return n % 2 === 0 ? n : n * 2;
3+
}

solution/2400-2499/2414.Length of the Longest Alphabetical Continuous Substring/README.md

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,17 +134,66 @@ func max(a, b int) int {
134134
}
135135
```
136136

137+
### **C**
138+
139+
```c
140+
#define max(a,b) (((a) > (b)) ? (a) : (b))
141+
142+
int longestContinuousSubstring(char *s) {
143+
int n = strlen(s);
144+
int i = 0;
145+
int res = 1;
146+
for (int j = 1; j < n; j++) {
147+
if (s[j] - s[j - 1] != 1) {
148+
res = max(res, j - i);
149+
i = j;
150+
}
151+
}
152+
return max(res, n - i);
153+
}
154+
```
155+
137156
### **TypeScript**
138157
139158
```ts
159+
function longestContinuousSubstring(s: string): number {
160+
const n = s.length;
161+
let res = 1;
162+
let i = 0;
163+
for (let j = 1; j < n; j++) {
164+
if (s[j].charCodeAt(0) - s[j - 1].charCodeAt(0) !== 1) {
165+
res = Math.max(res, j - i);
166+
i = j;
167+
}
168+
}
169+
return Math.max(res, n - i);
170+
}
171+
```
140172

173+
### **Rust**
174+
175+
```rust
176+
impl Solution {
177+
pub fn longest_continuous_substring(s: String) -> i32 {
178+
let s = s.as_bytes();
179+
let n = s.len();
180+
let mut res = 1;
181+
let mut i = 0;
182+
for j in 1..n {
183+
if s[j] - s[j - 1] != 1 {
184+
res = res.max(j - i);
185+
i = j;
186+
}
187+
}
188+
res.max(n - i) as i32
189+
}
190+
}
141191
```
142192

143193
### **...**
144194

145195
```
146196
147-
148197
```
149198

150199
<!-- tabs:end -->

solution/2400-2499/2414.Length of the Longest Alphabetical Continuous Substring/README_EN.md

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,17 +121,66 @@ func max(a, b int) int {
121121
}
122122
```
123123

124+
### **C**
125+
126+
```c
127+
#define max(a,b) (((a) > (b)) ? (a) : (b))
128+
129+
int longestContinuousSubstring(char *s) {
130+
int n = strlen(s);
131+
int i = 0;
132+
int res = 1;
133+
for (int j = 1; j < n; j++) {
134+
if (s[j] - s[j - 1] != 1) {
135+
res = max(res, j - i);
136+
i = j;
137+
}
138+
}
139+
return max(res, n - i);
140+
}
141+
```
142+
124143
### **TypeScript**
125144
126145
```ts
146+
function longestContinuousSubstring(s: string): number {
147+
const n = s.length;
148+
let res = 1;
149+
let i = 0;
150+
for (let j = 1; j < n; j++) {
151+
if (s[j].charCodeAt(0) - s[j - 1].charCodeAt(0) !== 1) {
152+
res = Math.max(res, j - i);
153+
i = j;
154+
}
155+
}
156+
return Math.max(res, n - i);
157+
}
158+
```
127159

160+
### **Rust**
161+
162+
```rust
163+
impl Solution {
164+
pub fn longest_continuous_substring(s: String) -> i32 {
165+
let s = s.as_bytes();
166+
let n = s.len();
167+
let mut res = 1;
168+
let mut i = 0;
169+
for j in 1..n {
170+
if s[j] - s[j - 1] != 1 {
171+
res = res.max(j - i);
172+
i = j;
173+
}
174+
}
175+
res.max(n - i) as i32
176+
}
177+
}
128178
```
129179

130180
### **...**
131181

132182
```
133183
134-
135184
```
136185

137186
<!-- tabs:end -->
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#define max(a,b) (((a) > (b)) ? (a) : (b))
2+
3+
int longestContinuousSubstring(char *s) {
4+
int n = strlen(s);
5+
int i = 0;
6+
int res = 1;
7+
for (int j = 1; j < n; j++) {
8+
if (s[j] - s[j - 1] != 1) {
9+
res = max(res, j - i);
10+
i = j;
11+
}
12+
}
13+
return max(res, n - i);
14+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
impl Solution {
2+
pub fn longest_continuous_substring(s: String) -> i32 {
3+
let s = s.as_bytes();
4+
let n = s.len();
5+
let mut res = 1;
6+
let mut i = 0;
7+
for j in 1..n {
8+
if s[j] - s[j - 1] != 1 {
9+
res = res.max(j - i);
10+
i = j;
11+
}
12+
}
13+
res.max(n - i) as i32
14+
}
15+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function longestContinuousSubstring(s: string): number {
2+
const n = s.length;
3+
let res = 1;
4+
let i = 0;
5+
for (let j = 1; j < n; j++) {
6+
if (s[j].charCodeAt(0) - s[j - 1].charCodeAt(0) !== 1) {
7+
res = Math.max(res, j - i);
8+
i = j;
9+
}
10+
}
11+
return Math.max(res, n - i);
12+
}

0 commit comments

Comments
 (0)