码迷,mamicode.com
首页 > 其他好文 > 详细

(leetcode)Longest Common Prefix

时间:2015-05-18 22:56:09      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:

class Solution {
public:
    string longestCommonPrefix(vector<string>& strs) {
		int len = strs.size();
		if(len == 0) return "";
		string prefix = strs[0];
		for(int i = 1;i < len;i++)
		{
			if(prefix.length() == 0 || strs[i].length() == 0) return "";
			int slen = prefix.length() < strs[i].length() ? prefix.length():strs[i].length();
			int j;
			for(j = 0;j<slen;j++)
			{
				if(prefix[j] != strs[i][j]) break; 
			}
			prefix = prefix.substr(0,j);
		}
		return prefix;
    }
};

  

(leetcode)Longest Common Prefix

标签:

原文地址:http://www.cnblogs.com/chdxiaoming/p/4513095.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!