首页 > 其他 > 详细

(leetcode)Longest Common Prefix

时间:2015-05-18 22:53:02      阅读:189      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!