首页 > 其他 > 详细

[LintCode] 最长公共前缀

时间:2015-06-29 16:34:28      阅读:187      评论:0      收藏:0      [点我收藏+]
 1 class Solution {
 2 public:    
 3     /**
 4      * @param strs: A list of strings
 5      * @return: The longest common prefix
 6      */
 7     string longestCommonPrefix(vector<string> &strs) {
 8         // write your code here
 9         string lcp = "";
10         if (strs.empty()) return lcp;
11         for (int i = 0; i < (int)strs[0].length(); i++) {
12             int pos = lcp.length();
13             char letter = strs[0][pos];
14             for (int j = 1; j < (int)strs.size(); j++)
15                 if (strs[j].length() == pos || strs[j][pos] != letter)
16                     return lcp;
17             lcp += letter;
18         }
19         return lcp;
20     }
21 };

 

[LintCode] 最长公共前缀

原文:http://www.cnblogs.com/jcliBlogger/p/4607569.html

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