/*
目的:搞懂 vector<string> a a[0].size() 和a.size()的区别
2019.5.9
结论:
1、a[0].size 计算存放在vector中第一个元素有多长
2、a.size 计算存放了多少个元数在vector中
3、可以将vector<string> a 看作是二维数组
4、a[3][2]=s a[4][4]=j
a[行][列]
行->a.size()
列->a[某行].size()
*/
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
vector<string> a ={"aasd7777","bbasd","asdal","dasdas",
/*第四个元素:"a"*/
"adsajsadada","sda","dsa"};
int b = a[0].size();//计算第1个位置上总共多长 vector数组从0开始存放第一个元素
int c = a.size();//计算vector中总共多长
int d = a[6].size();//计算第7个位置
cout << a[3][2] << endl;//s
cout << a[4][4] << endl;//j
//超过第四个元数的长度 报错 subscript out of the range
cout << b<<endl;//8
cout << c << endl;//7
cout << d << endl;//3
return 0;
}
编写一个函数来查找字符串数组中的最长公共前缀。
如果不存在公共前缀,返回空字符串 “”。
示例 1:
输入: [“flower”,“flow”,“flight”]
输出: “fl”
示例 2:
输入: [“dog”,“racecar”,“car”]
输出: “”
解释: 输入不存在公共前缀。
说明:
所有输入只包含小写字母 a-z 。
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
string result="";
if(strs.empty())
return result;
int i=0;
while(i<strs[0].size())//看懂上面就懂这了
{
char c=strs[0][i];
for(int j=1;j<strs.size();j++)
{
if(strs[j][i]==c)
continue;
else
return result;
}
result+=c;
i++;
}
return result;
}
};