Skip to content

Commit 2b4c581

Browse files
committed
feat: add solutions to lc problems
- No.0005.Longest Palindromic Substring - No.0322.Coin Change - No.0540.Single Element in a Sorted Array - No.2311.Longest Binary Subsequence Less Than or Equal to K
1 parent 1f00bcb commit 2b4c581

File tree

13 files changed

+452
-5
lines changed

13 files changed

+452
-5
lines changed

solution/0000-0099/0005.Longest Palindromic Substring/README.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,108 @@ proc longestPalindrome(s: string): string =
207207
result = s[start ..< start+mx]
208208
```
209209

210+
### **JavaScript**
211+
212+
```js
213+
/**
214+
* @param {string} s
215+
* @return {string}
216+
*/
217+
var longestPalindrome = function (s) {
218+
let maxLength = 0,
219+
left = 0,
220+
right = 0;
221+
for (let i = 0; i < s.length; i++) {
222+
let singleCharLength = getPalLenByCenterChar(s, i, i);
223+
let doubleCharLength = getPalLenByCenterChar(s, i, i + 1);
224+
let max = Math.max(singleCharLength, doubleCharLength);
225+
if (max > maxLength) {
226+
maxLength = max;
227+
left = i - parseInt((max - 1) / 2);
228+
right = i + parseInt(max / 2);
229+
}
230+
}
231+
return s.slice(left, right + 1);
232+
};
233+
234+
function getPalLenByCenterChar(s, left, right) {
235+
// 中间值为两个字符,确保两个字符相等
236+
if (s[left] != s[right]) {
237+
return right - left; // 不相等返回为1个字符串
238+
}
239+
while (left > 0 && right < s.length - 1) {
240+
// 先加减再判断
241+
left--;
242+
right++;
243+
if (s[left] != s[right]) {
244+
return right - left - 1;
245+
}
246+
}
247+
return right - left + 1;
248+
}
249+
```
250+
251+
### **TypeScript**
252+
253+
```ts
254+
function longestPalindrome(s: string): string {
255+
const n = s.length;
256+
const isPass = (l: number, r: number) => {
257+
while (l < r) {
258+
if (s[l++] !== s[r--]) {
259+
return false;
260+
}
261+
}
262+
return true;
263+
};
264+
let res = s[0];
265+
for (let i = 0; i < n - 1; i++) {
266+
for (let j = n - 1; j > i; j--) {
267+
if (j - i < res.length) {
268+
break;
269+
}
270+
if (isPass(i, j)) {
271+
res = s.slice(i, j + 1);
272+
}
273+
}
274+
}
275+
return res;
276+
}
277+
```
278+
279+
### **Rust**
280+
281+
```rust
282+
impl Solution {
283+
pub fn longest_palindrome(s: String) -> String {
284+
let n = s.len();
285+
let s = s.as_bytes();
286+
let is_pass = |mut l, mut r| {
287+
while l < r {
288+
if s[l] != s[r] {
289+
return false;
290+
}
291+
l += 1;
292+
r -= 1;
293+
}
294+
true
295+
};
296+
let mut res = &s[0..1];
297+
for i in 0..n - 1 {
298+
for j in (i + 1..n).rev() {
299+
if res.len() > j - i {
300+
break;
301+
}
302+
if is_pass(i, j) {
303+
res = &s[i..=j];
304+
}
305+
}
306+
}
307+
res.into_iter().map(|c| char::from(*c)).collect()
308+
}
309+
}
310+
```
311+
210312
### **...**
211313

212314
```

solution/0000-0099/0005.Longest Palindromic Substring/README_EN.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,108 @@ proc longestPalindrome(s: string): string =
192192
result = s[start ..< start+mx]
193193
```
194194

195+
### **JavaScript**
196+
197+
```js
198+
/**
199+
* @param {string} s
200+
* @return {string}
201+
*/
202+
var longestPalindrome = function (s) {
203+
let maxLength = 0,
204+
left = 0,
205+
right = 0;
206+
for (let i = 0; i < s.length; i++) {
207+
let singleCharLength = getPalLenByCenterChar(s, i, i);
208+
let doubleCharLength = getPalLenByCenterChar(s, i, i + 1);
209+
let max = Math.max(singleCharLength, doubleCharLength);
210+
if (max > maxLength) {
211+
maxLength = max;
212+
left = i - parseInt((max - 1) / 2);
213+
right = i + parseInt(max / 2);
214+
}
215+
}
216+
return s.slice(left, right + 1);
217+
};
218+
219+
function getPalLenByCenterChar(s, left, right) {
220+
// 中间值为两个字符,确保两个字符相等
221+
if (s[left] != s[right]) {
222+
return right - left; // 不相等返回为1个字符串
223+
}
224+
while (left > 0 && right < s.length - 1) {
225+
// 先加减再判断
226+
left--;
227+
right++;
228+
if (s[left] != s[right]) {
229+
return right - left - 1;
230+
}
231+
}
232+
return right - left + 1;
233+
}
234+
```
235+
236+
### **TypeScript**
237+
238+
```ts
239+
function longestPalindrome(s: string): string {
240+
const n = s.length;
241+
const isPass = (l: number, r: number) => {
242+
while (l < r) {
243+
if (s[l++] !== s[r--]) {
244+
return false;
245+
}
246+
}
247+
return true;
248+
};
249+
let res = s[0];
250+
for (let i = 0; i < n - 1; i++) {
251+
for (let j = n - 1; j > i; j--) {
252+
if (j - i < res.length) {
253+
break;
254+
}
255+
if (isPass(i, j)) {
256+
res = s.slice(i, j + 1);
257+
}
258+
}
259+
}
260+
return res;
261+
}
262+
```
263+
264+
### **Rust**
265+
266+
```rust
267+
impl Solution {
268+
pub fn longest_palindrome(s: String) -> String {
269+
let n = s.len();
270+
let s = s.as_bytes();
271+
let is_pass = |mut l, mut r| {
272+
while l < r {
273+
if s[l] != s[r] {
274+
return false;
275+
}
276+
l += 1;
277+
r -= 1;
278+
}
279+
true
280+
};
281+
let mut res = &s[0..1];
282+
for i in 0..n - 1 {
283+
for j in (i + 1..n).rev() {
284+
if res.len() > j - i {
285+
break;
286+
}
287+
if is_pass(i, j) {
288+
res = &s[i..=j];
289+
}
290+
}
291+
}
292+
res.into_iter().map(|c| char::from(*c)).collect()
293+
}
294+
}
295+
```
296+
195297
### **...**
196298

197299
```

solution/0000-0099/0005.Longest Palindromic Substring/Solution.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,3 @@ function getPalLenByCenterChar(s, left, right) {
3434
}
3535
return right - left + 1;
3636
}
37-
38-
console.log(longestPalindrome('cbbd'));
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
impl Solution {
2+
pub fn longest_palindrome(s: String) -> String {
3+
let n = s.len();
4+
let s = s.as_bytes();
5+
let is_pass = |mut l, mut r| {
6+
while l < r {
7+
if s[l] != s[r] {
8+
return false;
9+
}
10+
l += 1;
11+
r -= 1;
12+
}
13+
true
14+
};
15+
let mut res = &s[0..1];
16+
for i in 0..n - 1 {
17+
for j in (i + 1..n).rev() {
18+
if res.len() > j - i {
19+
break;
20+
}
21+
if is_pass(i, j) {
22+
res = &s[i..=j];
23+
}
24+
}
25+
}
26+
res.into_iter().map(|c| char::from(*c)).collect()
27+
}
28+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function longestPalindrome(s: string): string {
2+
const n = s.length;
3+
const isPass = (l: number, r: number) => {
4+
while (l < r) {
5+
if (s[l++] !== s[r--]) {
6+
return false;
7+
}
8+
}
9+
return true;
10+
};
11+
let res = s[0];
12+
for (let i = 0; i < n - 1; i++) {
13+
for (let j = n - 1; j > i; j--) {
14+
if (j - i < res.length) {
15+
break;
16+
}
17+
if (isPass(i, j)) {
18+
res = s.slice(i, j + 1);
19+
}
20+
}
21+
}
22+
return res;
23+
}

solution/0300-0399/0322.Coin Change/README.md

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161

6262
因此 `dp[i][j] = min(dp[i - 1][j], dp[i][j - v] + 1)`
6363

64-
时间复杂度 `O(m*amount)`,其中 m 表示 coins 长度。
64+
时间复杂度:$O(m*amount)$,其中 m 表示 `coins` 长度。
6565

6666
<!-- tabs:start -->
6767

@@ -219,7 +219,33 @@ function coinChange(coins: number[], amount: number): number {
219219
}
220220
```
221221

222-
### **....**
222+
### **Rust**
223+
224+
```rust
225+
impl Solution {
226+
pub fn coin_change(coins: Vec<i32>, amount: i32) -> i32 {
227+
let n = coins.len();
228+
let amount = amount as usize;
229+
let mut dp = vec![amount + 1; amount + 1];
230+
dp[0] = 0;
231+
for i in 1..=amount {
232+
for j in 0..n {
233+
let coin = coins[j] as usize;
234+
if coin <= i {
235+
dp[i] = dp[i].min(dp[i - coin] + 1);
236+
}
237+
}
238+
}
239+
if dp[amount] > amount {
240+
-1
241+
} else {
242+
dp[amount] as i32
243+
}
244+
}
245+
}
246+
```
247+
248+
### **...**
223249

224250
```
225251

solution/0300-0399/0322.Coin Change/README_EN.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,32 @@ function coinChange(coins: number[], amount: number): number {
196196
}
197197
```
198198

199+
### **Rust**
200+
201+
```rust
202+
impl Solution {
203+
pub fn coin_change(coins: Vec<i32>, amount: i32) -> i32 {
204+
let n = coins.len();
205+
let amount = amount as usize;
206+
let mut dp = vec![amount + 1; amount + 1];
207+
dp[0] = 0;
208+
for i in 1..=amount {
209+
for j in 0..n {
210+
let coin = coins[j] as usize;
211+
if coin <= i {
212+
dp[i] = dp[i].min(dp[i - coin] + 1);
213+
}
214+
}
215+
}
216+
if dp[amount] > amount {
217+
-1
218+
} else {
219+
dp[amount] as i32
220+
}
221+
}
222+
}
223+
```
224+
199225
### **....**
200226

201227
```
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
impl Solution {
2+
pub fn coin_change(coins: Vec<i32>, amount: i32) -> i32 {
3+
let n = coins.len();
4+
let amount = amount as usize;
5+
let mut dp = vec![amount + 1; amount + 1];
6+
dp[0] = 0;
7+
for i in 1..=amount {
8+
for j in 0..n {
9+
let coin = coins[j] as usize;
10+
if coin <= i {
11+
dp[i] = dp[i].min(dp[i - coin] + 1);
12+
}
13+
}
14+
}
15+
if dp[amount] > amount {
16+
-1
17+
} else {
18+
dp[amount] as i32
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)