标签:leetcode
Write a function to find the longest common prefix string amongst an array of strings.
求最长公共前缀。
代码如下:
class Solution { public: string longestCommonPrefix(vector<string>& strs) { int length = strs.size(); if (length <= 0) return ""; string prefix = strs[0]; int i, j; for (i=1; i<length; i++) { string tmpStr = strs[i]; if (prefix.length()==0 || tmpStr.length()==0) return ""; int len = min(prefix.length(), tmpStr.length()); for (j=0; j<len; j++) { if (prefix[j] != tmpStr[j]) break; } prefix = prefix.substr(0,j); } return prefix; } };
版权声明:本文为博主原创文章,未经博主允许不得转载。
LeetCode 14: Longest Common Prefix
标签:leetcode
原文地址:http://blog.csdn.net/sunao2002002/article/details/47154561