Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- string longestCommonPrefix(vector<string>& strs) { // binary search can be used.
- int n = strs.size();
- int ans = 1;
- if(strs.empty())return "";
- for(int ans = 1;true;ans++){
- for(string s:strs){
- if(s.length() < ans || s[ans-1]!=strs[0][ans-1]){
- return s.substr(0,ans-1);
- }
- }
- }
- }
- };
Add Comment
Please, Sign In to add comment