Skip to content

Add Solution.go for 0005.Longest Palindromic Substring #181

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
merged 1 commit into from
Jun 29, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
Add Solution.go for 0005.Longest Palindromic Substring
  • Loading branch information
Yeqi Tao committed Jun 29, 2019
commit 7e2e39bafedce3ef317afa4afdde49da455330bc
28 changes: 28 additions & 0 deletions solution/0005.Longest Palindromic Substring/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
func longestPalindrome(s string) string {
length := len(s)
if length == 0 {
return ""
}
res := make([][]bool, length)
for i := 0; i < length; i++ {
res[i] = make([]bool, length)
}
max := 1
start := 0
for i := 0; i < length; i++ {
for j := 0; j <= i; j++ {
if i - j < 2 {
res[j][i] = s[j] == s[i]
} else {
res[j][i] = s[j] == s[i] && res[j+1][i-1]
}

if res[j][i] && max < i - j + 1 {
max = i - j + 1
start = j
}
}
}

return s[start:start + max]
}
1 change: 1 addition & 0 deletions solution/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
├── 0005.Longest Palindromic Substring
│   ├── README.md
│   ├── Solution.java
│   ├── Solution.go
│   └── Solution.js
├── 0006.ZigZag Conversion
│   ├── README.md
Expand Down