Skip to content

Latest commit

 

History

History
39 lines (34 loc) · 626 Bytes

14.最长公共前缀.md

File metadata and controls

39 lines (34 loc) · 626 Bytes
title categories
最长公共前缀
leetcode
leetcode题解
package main

import (
	"fmt"
	"sort"
)

/*
 * @lc app=leetcode.cn id=14 lang=golang
 *
 * [14] 最长公共前缀
 */

// @lc code=start
func longestCommonPrefix(strs []string) string {
	sort.Strings(strs)
	fmt.Printf("min = %s , max = %s", strs[0], strs[len(strs)-1])

	minStrs, maxStrs := strs[0], strs[len(strs)-1]
	commonStrs := make([]byte, 0)
	for i := 0; i < len(minStrs); i++ {
		if minStrs[i] == maxStrs[i] {
			commonStrs = append(commonStrs, minStrs[i])
		} else {
			break
		}
	}
	return string(commonStrs)
}

// @lc code=end