Leetcode_Longest Common Prefix

2015-07-20 17:19:33 · 作者: · 浏览: 5

题意为找到所有字符串的最长的共同前缀。

思路:

1.先遍历一遍,得出最短字符串的长度

2.对每一个字符串均与第一个字符串比较,出现不同,则得出最长公共前缀到此结束

class Solution {
public:
    string longestCommonPrefix(vector
  
    &strs) {
		
		int len=strs.size();
		int min=INT_MAX;
		string commomPrefix;
		if(len==0)
			return commomPrefix;
		for(int k=0;k