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

【LeetCode】14 - Longest Common Prefix

时间:2015-08-09 00:18:55      阅读:105      评论:0      收藏:0      [点我收藏+]

标签:

Write a function to find the longest common prefix string amongst an array of strings.

Solution:

 1 class Solution {
 2 public:
 3     string longestCommonPrefix(vector<string>& strs) {      //runtime:4ms
 4         string str="";
 5         if(strs.empty())return str;
 6         int index=0;
 7         
 8         while(true){    
 9             if(index>=strs[0].size())return str;
10             char c=strs[0][index];
11             for(int i=1;i<strs.size();i++){
12                 if(index>=strs[i].size()||strs[i][index]!=c)return str;
13             }
14             str+=c;
15             index++;
16         }
17         
18     }
19 };

 

【LeetCode】14 - Longest Common Prefix

标签:

原文地址:http://www.cnblogs.com/irun/p/4714246.html

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