前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >LeetCode14.最长公共前缀(Kotlin语言)

LeetCode14.最长公共前缀(Kotlin语言)

作者头像
一个会写诗的程序员
修改2023-09-21 17:01:34
3310
修改2023-09-21 17:01:34
举报

LeetCode14.最长公共前缀(Kotlin语言)

题目描述

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

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

示例 1:

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

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

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

解题思路

1.先计算出数组中的最短字符串长度 shortestStrLen 2.最长公共前缀 <= shortestStrLen 3.for len in 0..shortestStrLen 逐一遍历 strs 中的元素,比对是否相等 4.遇到不相等的 len, 就停止遍历,记录此时的 maxCommonLen = len. 5.注意考虑遍历完 shortestStrLen,都相等的情况, 此时 maxCommonLen = shortestStrLen

代码

class Solution {
    fun longestCommonPrefix(strs: Array<String>): String {
        if (strs.isEmpty()) return ""

        // 数组中的最短的字符串长度
        var shortestStrLen = Int.MAX_VALUE
        strs.forEach {
            shortestStrLen = Math.min(it.length, shortestStrLen)
        }
        if (shortestStrLen == 0) return ""

        val n = strs.size
        var maxCommonLen = 0

        var flag = true
        for (len in 0 until shortestStrLen) {
            for (i in 1 until n) {
                if (strs[i][len] != strs[0][len]) {
                    flag = false
                    break
                }
            }

            // 遍历中断
            if (!flag) {
                maxCommonLen = len
                break
            } else {
                // 全部遍历均相同
                maxCommonLen = shortestStrLen
            }
        }

        return strs[0].subSequence(0, maxCommonLen).toString()
        
    }
}

运行测试

fun main() {
    run {
        val strs = arrayOf("flower", "flow", "flight")
        val common = longestCommonPrefix(strs)
        println("common=$common, expected=fl")
    }

    run {
        val strs = arrayOf("dog", "racecar", "car")
        val common = longestCommonPrefix(strs)
        println("common=$common, expected=")
    }

    run {
        val strs = arrayOf("doger", "dogeracecar", "dogercar")
        val common = longestCommonPrefix(strs)
        println("common=$common, expected=doger")
    }

    run {
        val strs = arrayOf("abca", "aba", "aaab")
        val common = longestCommonPrefix(strs)
        println("common=$common, expected=a")
    }

    run {
        val strs = arrayOf("c", "acc", "ccc")
        val common = longestCommonPrefix(strs)
        println("common=$common, expected=")
    }

    run {
        val strs = arrayOf("aac", "acab", "aa", "abba", "aa")
        val common = longestCommonPrefix(strs)
        println("common=$common, expected=a")
    }
}

// 输出:
common=fl, expected=fl
common=, expected=
common=doger, expected=doger
common=a, expected=a
common=, expected=
common=a, expected=a

参考资料

https://leetcode-cn.com/problems/longest-common-prefix/solution/zui-chang-gong-gong-qian-zhui-kotlinyu-yan-by-chen/

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • LeetCode14.最长公共前缀(Kotlin语言)
    • 题目描述
      • 解题思路
        • 代码
        • 运行测试
        • 参考资料
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档