首页 > 其他 > 详细

【LeetCode】014. Longest Common Prefix

时间:2018-03-25 17:50:56      阅读:201      评论: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 n = strs.size();
 5         if (n < 1 || strs[0].empty())
 6             return "";
 7         string res;
 8         
 9         for (int j = 0; j < strs[0].size(); ++j) {
10             char c = strs[0][j];
11             for (int i = 1; i < strs.size(); ++i) {
12                 if (j >= strs[i].size() || strs[i][j] != c)
13                     return res;
14             }
15             
16             res += c;
17         }
18         
19         return res;
20     }
21 };

 

【LeetCode】014. Longest Common Prefix

原文:https://www.cnblogs.com/Atanisi/p/8645475.html

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