Longest Common Prefix
生活随笔
收集整理的這篇文章主要介紹了
Longest Common Prefix
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Write a function to find the longest common prefix string amongst an array of strings.
Subscribe?to see which companies asked this question
?就是比較vector<string>中,從頭開始字符串相同的部分。。
就是第一個字符串和下面的進行比較,取相同的部分。
class Solution { public:string longestCommonPrefix(vector<string> &strs) {if (strs.size() == 0)return "";string temp = strs[0];for (int i = 1; i < strs.size(); ++i){if (temp.length() == 0 || strs[i].length() == 0)return "";int len = temp.length() < strs[i].length() ? temp.length() : strs[i].length();int j;for (j = 0; j < len; ++j){if (temp[j] != strs[i][j]) ?break;}temp = temp.substr(0,j);}return temp;} };class Solution { public:string longestCommonPrefix(vector<string>& strs) {if (strs.size() == 0) return "";for (int i = 0; i < strs[0].size(); ++i) {for (int j = 0; j < strs.size() - 1; ++j) {if (i >= strs[j].size() || strs[j][i] != strs[j + 1][i]) {return strs[j].substr(0, i);}}}return strs[0];} };
總結
以上是生活随笔為你收集整理的Longest Common Prefix的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Laplacian Eigenmaps
- 下一篇: c++获取屏幕大小