14.最长公共前缀
题目链接
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
if(strs.size()==0)return "";
int l= strs[0].length();
for(int i=1;i<strs.size();i++){
int j=0;
while(j<strs[i].length()&&j<l){
if(strs[i][j]!=strs[0][j])break;
j++;
}
l=min(l,j);
}
return string(strs[0],0,l);
}
};
20.有效的括号
题目链接
class Solution {
public:
stack<char>ms;
char c;
bool isValid(string s) {
while(!ms.empty()){ms.pop();}
int l=s.length();
for(int i=0;i<l;i++){
if(s[i]=='(')ms.push('(');
else if(s[i]=='[')ms.push('[');
else if(s[i]=='{')ms.push('{');
else {
if(!ms.empty())c=ms.top();
else return false;
if(s[i]==')'&&c=='('){ms.pop();}
else if(s[i]==']'&&c=='['){ms.pop();}
else if(s[i]=='}'&&c=='{'){ms.pop();}
else return false;
}
}
return ms.empty();
}
};