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

[LeetCode] 14. Longest Common Prefix

时间:2016-10-02 00:10:17      阅读:156      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!