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

[pull] master from youngyangyang04:master #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 6 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Jun 18, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions problems/0647.回文子串.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,63 @@ const countSubstrings = (s) => {
}
```

TypeScript:

> 动态规划:
```typescript
function countSubstrings(s: string): number {
/**
dp[i][j]: [i,j]区间内的字符串是否为回文(左闭右闭)
*/
const length: number = s.length;
const dp: boolean[][] = new Array(length).fill(0)
.map(_ => new Array(length).fill(false));
let resCount: number = 0;
// 自下而上,自左向右遍历
for (let i = length - 1; i >= 0; i--) {
for (let j = i; j < length; j++) {
if (
s[i] === s[j] &&
(j - i <= 1 || dp[i + 1][j - 1] === true)
) {
dp[i][j] = true;
resCount++;
}
}
}
return resCount;
};
```

> 双指针法:
```typescript
function countSubstrings(s: string): number {
const length: number = s.length;
let resCount: number = 0;
for (let i = 0; i < length; i++) {
resCount += expandRange(s, i, i);
resCount += expandRange(s, i, i + 1);
}
return resCount;
};
function expandRange(s: string, left: number, right: number): number {
let palindromeNum: number = 0;
while (
left >= 0 && right < s.length &&
s[left] === s[right]
) {
palindromeNum++;
left--;
right++;
}
return palindromeNum;
}
```




-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

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