leetcode_14题——Longest Common Prefix(字符串)

Longest Common Prefix

Total Accepted: 44093 Total Submissions: 169565My Submissions

Question Solution

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

Hide Tags

String

Have you met this question in a real interview?

Yes

No

Discuss

这是一道简单题,找出一系列字符串中的最长的前缀字符串

#include<iostream>
#include<string>
#include <vector>
using namespace std;

string longestCommonPrefix(vector<string>& strs) {

	string result;
	if(strs.empty())
		return result;
	if(strs.size()==1)
	{
		result=strs[0];
		return result;
	}
	int len1=strs[0].size();
	for(int j=0;j<len1;++j)
	{
		result.push_back((strs[0])[j]);
		int len2=strs.size();
		for(int i=0;i<len2;++i)
		{
			int len3=strs[i].size();
			if(j>=len3)
			{
				result.pop_back();
				return result;
			}
			if((strs[0])[j]!=(strs[i])[j])
			{
				result.pop_back();
				return result;
			}

		}
	}
	return result;
}
int main()
{
	vector<string> str;
	str.push_back("asdf");
	str.push_back("asdgk");
	str.push_back("asd");

	cout<<longestCommonPrefix(str)<<endl;

}

  

时间: 2024-10-12 09:28:33

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

[LeetCode] Longest Common Prefix 字符串公有前序

Write a function to find the longest common prefix string amongst an array of strings. Hide Tags String 这是一道很简单的题目,判断输入的多个字符串的公有前序,简单的逻辑遍历查找就好. 算法流程: 判断输入的字符串数量,小于2时候做出相应返回. 获取最短字符串的长度. 设定标记flag,控制跳出循环,与长度返回的长度len. 在最短字符串长度的范围内循环. 循环中每次遍历全部字符串len 位的字

leetcode第14题--Longest Common Prefix

Problems:Write a function to find the longest common prefix string amongst an array of strings. 就是返回一个字符串数组的所有的公共前缀.不难.我是已第一个字符串为参考,然后依次遍历其他的字符串,一旦遇到不同的.就break.然后返回第一个字符串的相应子序列. class Solution { public: string longestCommonPrefix(vector<string> &

【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】LeetCode——第14题:Longest Common Prefix

14. Longest Common Prefix My Submissions Question Editorial Solution Total Accepted: 97052 Total Submissions: 345681 Difficulty: Easy Write a function to find the longest common prefix string amongst an array of strings. Subscribe to see which compan

leetcode Longest Common Prefix 多个字符串的最长字串

1 public class Solution { 2 public String get(String a,String b) 3 { 4 5 if(a==""||b=="") return ""; 6 int len1=a.length(); 7 int len2=b.length(); 8 int len=len1; 9 if(len>=len2) len=len2; 10 String s=""; 11 for(

LeetCode第[14]题(Java): Longest Common Prefix

题目:最长公共前缀 难度:EASY 题目内容: 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: ["flow

14. 字符串数组的最长公共前缀 Longest Common Prefix

Write a function to find the longest common prefix string amongst an array of strings. public class Solution { public string LongestCommonPrefix(string[] strs) { StringBuilder result = new StringBuilder(); if (strs != null && strs.Length > 0) {

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