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

Longest Common Prefix

时间:2014-09-07 14:46:45      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   ar   for   art   div   cti   

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 string(""); }
 5         int start = -1;
 6         while( ++start < strs[0].length() ) {
 7             for( int i = (int)strs.size()-1; i > 0; --i ) {
 8                 if( strs[i][start] != strs[0][start] ) { return strs[0].substr( 0, start ); }
 9             }
10         }
11         return strs[0].substr( 0, start );
12     }
13 };

 

Longest Common Prefix

标签:style   blog   color   io   ar   for   art   div   cti   

原文地址:http://www.cnblogs.com/moderate-fish/p/3960360.html

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