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         int length =strs.length,strLength=0;
 4         StringBuffer sBuffer=new StringBuffer();
 5         boolean isSame=false;
 6         if(strs.length!=0)//考虑字符串数组可能为空情况
 7             strLength=strs[0].length();
 8         if (strs.length==1) {//考虑字符串数组为一个的情况
 9             return strs[0];
10         }
11         else{
12             for (int i = 0; i < length - 1; i++) {
13                 if (strs[i].length() == 0)//考虑某个字符串为空的情况
14                     return "";
15                 if (strLength > strs[i + 1].length())//求出最短字符串的长度
16                     strLength = strs[i + 1].length();
17
18             }
19             for (int i = 0; i < strLength; i++) {
20                 for (int j = 0; j < length - 1; j++) {
21                     if (strs[j].charAt(i) == strs[j + 1].charAt(i)) {
22                         isSame = true;
23                     }
24                     else{
25                         isSame=false;
26                     }
27                 }
28                 if (isSame)
29                     sBuffer.append(strs[0].charAt(i));
30                 else
31                     break;
32             }
33             return sBuffer.toString();
34     }
35 }
时间: 2024-08-02 06:59:06

LeetCode记录之14——Longest Common Prefix的相关文章

[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===注意边

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

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

Write a function to find the longest common prefix string amongst an array of strings. Solution: 1 class Solution { 2 public: 3 string longestCommonPrefix(vector<string>& strs) { //runtime:4ms 4 string str=""; 5 if(strs.empty())return

Java [leetcode 14] Longest Common Prefix

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

[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这样单词中只有一个字母不同的单词进行

LeetCode 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) { string result = ""; if (!strs.size())return resul