Skip to content
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

无重复字符的最长子串(golang) #42

Open
Vexth opened this issue May 26, 2021 · 0 comments
Open

无重复字符的最长子串(golang) #42

Vexth opened this issue May 26, 2021 · 0 comments

Comments

@Vexth
Copy link
Owner

Vexth commented May 26, 2021

func longestPalindrome(s string) string {
	l := len(s)
	if l < 2 {
		return s
	}

	start, end := 0, 0
	for i := 0; i < l; {
		left, right := i, i
		for right < l-1 && s[right] == s[right+1] {
			right++
		}
		i = right + 1
		for left > 0 && right < l-1 && s[left-1] == s[right+1] {
			left--
			right++
		}
		if end < right-left {
			start = left
			end = right - left
		}
	}
	return s[start : start+end+1]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant