标签:
Problem:Write a function to find the longest common prefix string amongst an array of strings.
Solution:题意要求求取字符串数组的最长公共前缀子串。从位置0开始,对每一个位置比较所有的字符串,直到遇到不匹配的字符串位置
class Solution { public: string longestCommonPrefix(vector<string>& strs) { if(strs.empty()) return ""; for(int index=0;index<strs[0].size();index++) for(int i=1;i<strs.size();i++) if(strs[i][index]!=strs[0][index]) return strs[0].substr(0,index); return strs[0]; } };
LeetCode:Longest Common Prefix
标签:
原文地址:http://www.cnblogs.com/xiaoying1245970347/p/4639240.html