LeetCode //14. Longest Common Prefix

这篇具有很好参考价值的文章主要介绍了LeetCode //14. Longest Common Prefix。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

14. Longest Common Prefix

Write a function to find the longest common prefix string amongst an array of strings.

If there is no common prefix, return an empty string “”.

 

Example 1:

Input: strs = [“flower”,“flow”,“flight”]
Output: “fl”

Example 2:

Input: strs = [“dog”,“racecar”,“car”]
Output: “”
Explanation: There is no common prefix among the input strings.

Constraints:
  • 1 <= strs.length <= 200
  • 0 <= strs[i].length <= 200
  • strs[i] consists of only lowercase English letters.

From: LeetCode
Link: 14. Longest Common Prefix文章来源地址https://www.toymoban.com/news/detail-607705.html


Solution:

Ideas:
In this code, we first check if the array of strings is empty. If it is, we return an empty string. Then, for each character in the first string, we compare it with the corresponding character in every other string. If we find a character that doesn’t match or we reach the end of any of the strings, we return the common prefix found so far. If we traverse the entire first string without finding any non-matching characters, we return the first string as the common prefix.
Code:
char * longestCommonPrefix(char ** strs, int strsSize){
    if(strsSize == 0) return "";
    
    for(int index = 0; index < strlen(strs[0]); index++){
        for(int i = 1; i < strsSize; i++){
            if(index == strlen(strs[i]) || strs[i][index] != strs[0][index]){
                char* res = (char*)malloc(sizeof(char) * (index + 1));
                strncpy(res, strs[0], index);
                res[index] = '\0';
                return res;
            }
        }
    }
    
    char* res = (char*)malloc(sizeof(char) * (strlen(strs[0]) + 1));
    strncpy(res, strs[0], strlen(strs[0]));
    res[strlen(strs[0])] = '\0';
    return res;
}

到了这里,关于LeetCode //14. Longest Common Prefix的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【递增栈】个人练习-Leetcode-845. Longest Mountain in Array

    题目链接:https://leetcode.cn/problems/longest-mountain-in-array/ 题目大意:给出一个数组 arr[] ,定义【山数组】为【长度大于等于3】【前面部分递增,后面部分递减,存在一个山峰元素】的数组。求 arr[] 中的最长的是【山数组】的子列。 思路:递增栈当然可以,不过刚好想到另一个

    2024年02月06日
    浏览(41)
  • LeetCode 2000. Reverse Prefix of Word

    Given a  0-indexed  string  word  and a character  ch ,  reverse  the segment of  word  that starts at index  0  and ends at the index of the  first occurrence  of  ch  ( inclusive ). If the character  ch  does not exist in  word , do nothing. For example, if  word = \\\"abcdefd\\\"  and  ch = \\\"d\\\" , then you should  reverse  the segment that

    2024年02月09日
    浏览(27)
  • LeetCode 1048. Longest String Chain【记忆化搜索,动态规划,哈希表,字符串】中等

    本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,

    2024年02月05日
    浏览(33)
  • Leetcode 3045. Count Prefix and Suffix Pairs II

    Leetcode 3045. Count Prefix and Suffix Pairs II 1. 解题思路 2. 代码实现 题目链接:3045. Count Prefix and Suffix Pairs II 这一题的话思路上就是一个Trie树的思路来寻找前序字符,然后由于题目要求要同时满足前序和后序两个条件,因此找到每一个单词的前序子串之后再判断一下其是否同时为后

    2024年02月21日
    浏览(31)
  • LCS2 - Longest Common Substring II

    洛谷LCS2 - Longest Common Substring II 题目大意 给你一些字符串,求它们的最长公共子串。 字符串个数不超过 10 10 10 ,每个字符串的长度不超过 100000 100000 100000 。 题解 可以先看看LCS - Longest Common Substring。 这题与上面那题类似,只不过要多一些操作。 首先,用第一个字符串建一个

    2024年02月12日
    浏览(22)
  • 1143. Longest Common Subsequence 1035. Uncrossed Lines 53. Maximum Subarray

    1143. Longest Common Subsequence Given two strings  text1  and  text2 , return  the length of their longest  common subsequence .  If there is no  common subsequence , return  0 . A  subsequence  of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remai

    2024年02月03日
    浏览(34)
  • LeetCode2085. Count Common Words With One Occurrence

    Given two string arrays words1 and words2, return the number of strings that appear exactly once in each of the two arrays. Example 1: Input: words1 = [“leetcode”,“is”,“amazing”,“as”,“is”], words2 = [“amazing”,“leetcode”,“is”] Output: 2 Explanation: “leetcode” appears exactly once in each of the two arrays. We count this s

    2024年01月16日
    浏览(27)
  • 【LeetCode】HOT 100(14)

    精选 100 道力扣(LeetCode)上最热门的题目,适合初识算法与数据结构的新手和想要在短时间内高效提升的人,熟练掌握这 100 道题,你就已经具备了在代码世界通行的基本能力。 目录 题单介绍: 题目:85. 最大矩形 - 力扣(Leetcode) 题目的接口: 解题思路: 代码: 过过过过

    2024年02月16日
    浏览(28)
  • leetcode14. 最长公共前缀

    题目 编写一个函数来查找字符串数组中的最长公共前缀。 如果不存在公共前缀,返回空字符串 “”。 解题方法: 1.首先找到数组中长度最短的数据,与数组第一个数进行交换(公共前缀的长度肯定不会大于列表中长度最短的字符串) 2.接着 因为求最长公共前缀,将数组第

    2024年01月21日
    浏览(36)
  • Leetcode 3.14

    1.二叉树的层序遍历 二叉树的层序遍历 二叉树的层序遍历可以用先进先出的队列来实现。 将每一层的所有node都添加到队列中,记录下当前队列的长度,即该层的元素数量; 遍历队列中当前层的元素,添加该层每一个元素的左右子树。 2.验证二叉搜索树 验证二叉搜索树 如果

    2024年03月15日
    浏览(26)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包