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

最长公共前缀 go版本代码有误 #58

Open
cxz66666 opened this issue Feb 8, 2022 · 0 comments
Open

最长公共前缀 go版本代码有误 #58

cxz66666 opened this issue Feb 8, 2022 · 0 comments

Comments

@cxz66666
Copy link

cxz66666 commented Feb 8, 2022

给出的实例代码中 内循环没有判断是否超过字符串长度,如果给出[]string{"flower", "flow", "flow"} 这样的输入直接就panic了。
建议修改为

for i := range firstStr {
		for j := 1; j < l; j++ {
			if len(arr[j]) == i || arr[j][i] != firstStr[i] {
				return firstStr[:i]
			}
		}
	}
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