Skip to content

Instantly share code, notes, and snippets.

@kaseiwang
Created April 7, 2016 13:39
Show Gist options
  • Save kaseiwang/cec164960534af7ecac51ce78ca0dd2b to your computer and use it in GitHub Desktop.
Save kaseiwang/cec164960534af7ecac51ce78ca0dd2b to your computer and use it in GitHub Desktop.
class Solution {
public:
int maximalSquare(vector<vector<char>>& matrix) {
int m=matrix.size(), n=0, size=0;
if(m)
n=matrix[0].size();
else
return 0;
for(int i=0; i<m-size; i++){
for(int j=0; j<n-size; j++){
while(isVaildSquare(matrix, i, j, ++size)){}
size--;
}
}
return size*size;
}
inline bool isVaildSquare(vector<vector<char>>& matrix, int x, int y, int size){
if(matrix[x][y]=='0')
return false;
if(x+size>matrix.size())
return false;
if(y+size>matrix[0].size())
return false;
for(int i=size-1; i>=0; i--){
for(int j=size-1; j>=0; j--){
if(matrix[x+i][y+j]=='0'){
return false;
}
}
}
return true;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment