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

[LeetCode] Longest Common Prefix

时间:2015-04-16 21:20:24      阅读:116      评论: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         if (strs.empty()) return "";
 5         
 6         for (int i = 0; i < strs[0].size(); ++i)
 7             for (int j = 1; j < strs.size(); ++j) {
 8                 if (strs[j][i] != strs[0][i]) return strs[0].substr(0,i); 
 9             }
10         
11         return strs[0];
12     }
13 };

 

[LeetCode] Longest Common Prefix

标签:

原文地址:http://www.cnblogs.com/vincently/p/4433062.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!