14. Longest Common Prefix (截取字符串)

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

char* longestCommonPrefix(char** strs, int strsSize) {
    if(strsSize==0) return "";

    char* ret = strs[0];
    int i, j;
    int cmpLen;
    for(int i = 1; i < strsSize; i++){ //traverse strs
        j = 0;
        cmpLen = (strlen(strs[i]) > strlen(ret))?strlen(ret):strlen(strs[i]);
        while(j < cmpLen && strs[i][j]==ret[j]) j++;
        ret[j] = ‘\0‘;
    }
    return ret;
}
时间: 2024-10-17 14:54:16

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. 题目大意:求一组字符串的最长前缀. 代码如下: class Solution { public:     string longestCommonPrefix(vector<string>& strs) {         if(strs.size() == 0)

14. Longest Common Prefix【leetcode】

14. Longest Common Prefix Write a function to find the longest common prefix string amongst an array of strings. 寻找一个数组中最长的公共前缀 例如["baaa","caaabbb","aaaa"]输出"aaa" 结题思路: 判断非空的情况在进行计算 取第一个字符串最为标杆进行对比,因为最终结果一定在第一位中 用第一

No.14 Longest Common Prefix

No.14 Longest Common Prefix Write a function to find the longest common prefix string amongst an array of strings. 求一组string的最长公共前缀想法:找到最短的那个,然后依次对比 典型的字符串操作,从前向后比较即可 1 #include "stdafx.h" 2 #include <string> 3 #include <vector> 4 #i

[LeetCode][Python]14: Longest Common Prefix

# -*- coding: utf8 -*-'''__author__ = '[email protected]'https://oj.leetcode.com/problems/longest-common-prefix/14: Longest Common Prefix Write a function to find the longest common prefix string amongst an array of strings.===Comments by Dabay===注意边

leedCode练题——14. Longest Common Prefix

1.题目 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: ["flower","flow","flight&qu

LeetCode记录之14——Longest Common Prefix

本题虽然是easy难度,题目也一目了然,问题就是在这里,需要考虑的特殊情况太多,太多限制.导致我一点点排坑,浪费了较多时间. Write a function to find the longest common prefix string amongst an array of strings. 编写一个函数来查找字符串数组中最长的公共前缀字符串. 1 class Solution { 2 public String longestCommonPrefix(String[] strs) { 3

Java [leetcode 14] Longest Common Prefix

小二好久没有更新博客了,真是罪过,最近在看linux的东西导致进度耽搁了,所以今晚睡觉前怒刷一题! 问题描述: Write a function to find the longest common prefix string amongst an array of strings. 解题思路: 该问题就是找到所有数组字符串里面的最长相同前字串.所以我的思路是先找到数组中最短的那个字符串,然后每次比较的时候最多循环该长度就行,这样避免字符串下标溢出的问题.设置StringBuilder对象用于存

14.Longest Common Prefix (String)

Write a function to find the longest common prefix string amongst an array of strings. class Solution { public: string longestCommonPrefix(vector<string>& strs) { int vSize = strs.size(); string result = ""; if(vSize == 0) return resul

[LeetCode][14]Longest Common Prefix解析 两种算法和底层源码的深入对比-Java实现

Q: Write a function to find the longest common prefix string amongst an array of strings. A: 这题的大概意思就是说给你一组字符串找出其中最长的哪个通用的前缀出来.这个东西不难找,但是如何找的又快又好不简单.其实这题本来就是easy题,但是却让我联想到了<数据结构与算法分析>上的一道题目,那道题目是这样的: 给一个8900个字的字典,从中间找出类似abc.bbc.abb这样单词中只有一个字母不同的单词进行