【Leetcode-easy】Longest Common Prefix

思路:每次从字符数组中读取两个字符串比较。需要注意输入字符串为空,等细节。

 1     public String longestCommonPrefix(String[] strs) {
 2         if(strs==null||strs.length==0){
 3             return "";
 4         }
 5         int count=strs[0].length();
 6         for(int i=1;i<strs.length;i++){
 7             String str1=strs[i-1];
 8             String str2=strs[i];
 9             int len=Math.min(str1.length(),str2.length());
10             if(len<count){
11                 count=len;
12             }
13             int comNum=0;
14             for(int j=0;j<count;j++){
15                 if(str1.charAt(j)==str2.charAt(j)){
16                     comNum++;
17                 }else{
18                     break;
19                 }
20             }
21             if(comNum<count){
22                 count=comNum;
23             }
24
25         }
26         if(count==0){
27             return "";
28         }
29         return strs[0].substring(0, count);
30     }
时间: 2024-10-11 00:03:47

【Leetcode-easy】Longest Common Prefix的相关文章

【LeetCode算法】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"] Output: "fl" Exa

【leetcode刷题笔记】Longest Common Prefix

Write a function to find the longest common prefix string amongst an array of strings. 题解:以strs[0]为模板,每次挨个查看是否所有的串里面是否第i位上都和strs[0]一样,如果都一样,把i位置上的字符放到answer里面,i++,继续循环,否则返回当前的answer. 代码如下: 1 public class Solution { 2 public String longestCommonPrefix

【LeetCode OJ】Longest Consecutive Sequence

Problem Link: http://oj.leetcode.com/problems/longest-consecutive-sequence/ This problem is a classical problem where we can reduce the running time by the help of hash table. By given a list of numbers, we can find the longest consecutive sequence b

leetcode -- 刷效率 Longest Common Prefix

题目描述: Write a function to find the longest common prefix string amongst an array of strings. 很简单的一道题目,但是我写了3个不一样的版本,运行时间确实数倍之差..贴代码如下: 版本1: 这个版本的运行时间为  44 ms 版本2: 这个版本的运行时间为  16 ms 两者之间的区别便是:有无创建string ret返回字符串..  [updated]版本3:使用字符数组,时间同样是16ms,代码如下:

【LeetCode OJ 14】Longest Common Prefix

题目链接:https://leetcode.com/problems/longest-common-prefix/ 题目:Write a function to find the longest common prefix string amongst an array of strings. 解题思路:寻找字符串数组的最长公共前缀,将数组的第一个元素作为默认公共前缀,依次与后面的元素进行比较,取其公共部分,比较结束后,剩下的就是该字符串数组的最长公共前缀,示例代码如下: public clas

【Leetcode】Longest Common Prefix

题目链接:https://leetcode.com/problems/longest-common-prefix/ 题目: Write a function to find the longest common prefix string amongst an array of strings. 算法: [java] view plain copy public String longestCommonPrefix(String[] strs) { if (strs.length == 0) {

【LeetCode】 Longest Common Prefix

Longest Common Prefix Write a function to find the longest common prefix string amongst an array of strings. 题目的意图 It seems that it is not to check between pair of strings but on all the strings in the array. For example: {"a","a",&quo

[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 最长公共前缀

公众号:爱写bug 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"

leetcode笔记:Longest Common Prefix

一. 题目描述 Write a function to find the longest common prefix string amongst an array of strings. 二. 题目分析 题目的大意是,给定一组字符串,找出所有字符串的最长公共前缀. 对比两个字符串的最长公共前缀,其前缀的长度肯定不会超过两个字符串中较短的长度,设最短的字符串长度为n,那么只要比较这两个字符串的前n个字符即可. 使用变量prefix保存两个字符串的最长公共前缀,再将prefix作为一个新的字符串与