风雨雾凇 风雨雾凇
首页
  • 服务端

    • golang
  • 其他

    • leetcode
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

风雨雾凇

技术小渣渣
首页
  • 服务端

    • golang
  • 其他

    • leetcode
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • golang

  • 技术文档

  • GitHub技巧

  • Nodejs

  • 博客搭建

  • leetcode

    • 两数之和-1
    • 无重复字符的最长子串-3
    • 整数翻转-7
    • 回文数-9
    • 13-罗马数字转整数
    • 最长公共前缀
      • 思路
      • 代码
      • 官方最佳答案
    • 3Sum
    • Valid Parentheses
    • Merge Two Sorted Lists
    • Remove Duplicates from Sorted Array
    • Remove Element
    • Implement strStr()
    • Search Insert Position
    • Count and Say
    • 字符串相乘
    • Maximum Subarray
    • Length of Last Word
    • Plus One
    • Add Binary
    • Sqrt(x)
    • 翻转字符串里的单词
    • 字符串的排列
  • 机器学习

  • 技术
  • leetcode
风雨雾凇
2018-12-14
目录

最长公共前缀

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

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

示例 1:
输入: ["flower","flow","flight"]
输出: "fl"

示例 2:
输入: ["dog","racecar","car"]
输出: ""
1
2
3
4
5
6
7

说明:

所有输入只包含小写字母 a-z 。

题目链接:点击这里~ (opens new window)

# 思路

该题可直接暴力破解,有横向和纵向两种思路:

  1. 先记录第一个字符串,然后以第一个字符串的值去匹配剩下的字符串,遇到不同的则截取匹配下一个。例如:字符串数组为 ["abcde", "abced", "abbcde"] 。则当第一个去匹配第二个字符串的时候遇到e和第一个值d不同。故截取剩余abc,同理abc与abb截取后得到ab。
  2. 依次取每个字符串的相同位置字符作对比,不同则返回。

# 代码

采用横向对比解。

public class 最长公共前缀 {
    public static String longestCommonPrefix(String[] strs) {
        // 代码健壮性判断
        if(strs.length == 0){
            return "";
        }
        String res = strs[0];
        int len;
        for(int i = 1; i < strs.length; i++){
            len = Math.min(res.length(), strs[i].length());
            // 避免aa 和 a 的情况
            res = res.substring(0, len);
            for (int j = 0; j < len; j++){
                if(strs[0].charAt(j)!=strs[i].charAt(j)) {
                    res = res.substring(0, j);
                    break;
                }
            }
        }
        return res;
    }

    public static void main(String[] args) {
        System.out.println(longestCommonPrefix(new String[]{"aa", "a"}));
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

注意点:

  1. 需要做好代码健壮性,例如输入为 String[]{}。
  2. 特殊情况,例如 aa 和 a 的公共前缀为 a。需取最小值。

# 官方最佳答案

采用了横向对比。

class Solution {
  public String longestCommonPrefix(String[] strs) {
        if (strs.length == 0) {
            return "";
        }
		
        int shortest = strs[0].length();
        for (int i = 1; i < strs.length; i++) {
            shortest = Math.min(shortest, strs[i].length());
        }
        
        String result = "";
        for (int i = 0; i < shortest; i++) {
            char ch = strs[0].charAt(i);
            for (int j = 1; j < strs.length; j++) {
                if (strs[j].charAt(i) != ch) {
                    return result;
                }
            }
            result += ch;
        }
        return result;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
编辑 (opens new window)
#leetcode#每日训练#算法#面试#学习笔记
上次更新: 2023/02/17, 16:53:03
13-罗马数字转整数
3Sum

← 13-罗马数字转整数 3Sum→

最近更新
01
builtin
02-12
02
导读
02-12
03
13-罗马数字转整数
01-30
更多文章>
Theme by Vdoing | Copyright © 2017-2023 风雨雾凇 | 粤ICP备16018321号-2
博客内容遵循署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0) 协议
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式