首页 > 其他 > 详细

[LeetCode]Longest Common Prefix

时间:2015-11-12 11:31:58      阅读:261      评论: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         int len = strs.size();
 5         if (len == 0) {
 6             return string("");
 7         }
 8         string result{strs[0]};
 9         
10         for (int i = 1; i < len; ++i) {
11             int len_c = min(result.size(), strs[i].size());
12             int j = 0;
13             for (; j < len_c; ++j) {
14                 if (result[j] != strs[i][j]) {
15                     break;
16                 }
17             }
18             
19             result = result.substr(0, j);
20         }
21         
22         return result;
23     }
24 };

 

[LeetCode]Longest Common Prefix

原文:http://www.cnblogs.com/skycore/p/4958136.html

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