首页 > 其他 > 详细

[LeetCode] 14. Longest Common Prefix

时间:2016-10-02 00:10:10      阅读:271      评论:0      收藏:0      [点我收藏+]

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

 1 class Solution {
 2 public:
 3     string longestCommonPrefix(vector<string>& strs) {
 4         string prefix="";
 5         if(strs.size()==0)  return prefix;
 6 
 7         /** check char by char, for each char, check all the string word **/
 8         for(int k=0; k<strs[0].size(); k++){
 9             int i=1;
10             for(; i<strs.size() && strs[i].size()>k; i++){
11                 if(strs[i][k]!=strs[0][k])
12                     return prefix;
13             }
14             if(i==strs.size()) prefix+=strs[0][k];
15         }
16         return prefix;
17     }
18 };

 

[LeetCode] 14. Longest Common Prefix

原文:http://www.cnblogs.com/amadis/p/5926481.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!