FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
leetcode/code/lc647.java at master · mJackie/leetcode · GitHub
mJackie
/
leetcode
Public
Notifications
You must be signed in to change notification settings
Fork
135
Star
405
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
leetcode
/
code
/
lc647.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
29 lines (29 loc) · 1.01 KB
Breadcrumbs
leetcode
/
code
/
lc647.java
Copy path
File metadata and controls
29 lines (29 loc) · 1.01 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package
code
;
/*
* 647. Palindromic Substrings
* 题意:一个字符串里有多少个回文子串
* 难度:Medium
* 分类:String, Dynamic Programming
* 思路:时间为N^2,用二维dp空间复杂度是N^2. 该题直接让判断是否回文,直接选择找中心字符,向两边拓展,空间为O(1)
* Tips:lc5, lc9, lc125, lc131, lc234, lc647
*/
public
class
lc647
{
public
static
void
main
(
String
[]
args
) {
System
.
out
.
println
(
countSubstrings
(
"aaa"
));
}
public
static
int
countSubstrings
(
String
s
) {
int
count
=
0
;
for
(
int
i
=
0
;
i
<
s
.
length
() ;
i
++) {
for
(
int
j
=
0
;
j
<
2
;
j
++) {
//中心可能是两个字符,也可能是一个字符
int
left
=
i
;
int
right
=
left
+
j
;
while
(
left
>=
0
&&
right
<
s
.
length
() &&
s
.
charAt
(
left
)==
s
.
charAt
(
right
)){
count
++;
left
--;
right
++;
}
}
}
return
count
;
}
}
Back
|
FazBrowse Home
|
New Git URL