Skip to content

最长公共前缀(golang) #41

Description

@Vexth
func longestCommonPrefix(strs []string) string {
	if len(strs) == 0 {
		return ""
	}

	prefix := strs[0]

	for i := 1; i < len(strs); i++ {
		j := 0
		for j = 0; j < len(prefix) && j < len(strs[i]); j++ {

			if prefix[j] != strs[i][j] {
				break
			}

		}
		prefix = prefix[0:j]
		if prefix == "" {
			return prefix
		}
	}

	return prefix

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions