topic11

package
v0.0.0-...-b071cee Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 9, 2023 License: GPL-3.0 Imports: 1 Imported by: 0

README

最长公共前缀

1. 题目描述

编写一个函数来查找字符串数组中的最长公共前缀。

如果不存在公共前缀,返回空字符串 ""。

2. 示例

示例1

输入:strs = ["flower","flow","flight"]
输出:"fl"

示例2

输入:strs = ["dog","racecar","car"]
输出:""
解释:输入不存在公共前缀。

提示:

  • 1 <= strs.length <= 200
  • 0 <= strs[i].length <= 200
  • strs[i] 仅由小写英文字母组成

3. 解题

谨防超时

  • 纵向扫描法 依次对比数组中每个字符串的当前位,若遇到不一致,则返回当前已求得的最长公共前缀,若全部一致,则继续检查下一个位置上的字符,检查过程中要注意防止数组越界

  • 横向扫描法, 首先对前两个元素求最长公共前缀,求出的公共前缀再与下一个元素求公共前缀,这样一直求到最后,得到的公共前缀就是最长公共前缀

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL