1. Maximal Square
这道题通过记录边长的方式。对于m[i][j],如果是0,则把res矩阵这个元素设为0,如果是1则记录m[i-1][j], m[i][j-1], m[i-1][j-1]的最小值+1.
public class Solution {
/**
* @param matrix: a matrix of 0 and 1
* @return: an integer
*/
public int maxSquare(int[][] matrix) {
// write your code here
int ans = 0;
int m = matrix.length;
int n;
if(m > 0)
n = matrix[0].length;
else
return ans;
int [][]res = new int [m][n];
for(int i = 0; i < m; i++){
res[i][0] = matrix[i][0];
ans = Math.max(res[i][0] , ans);
for(int j = 1; j < n; j++) {
if(i > 0) {
if(matrix[i][j] > 0) {
res[i][j] = Math.min(res[i - 1][j],Math.min(res[i][j-1], res[i-1][j-1])) + 1;
} else {
res[i][j] = 0;
}
}
else {
res[i][j] = matrix[i][j];
}
ans = Math.max(res[i][j], ans);
}
}
return ans * ans;
}
}
2. Largest Rectangle in Histogram
历遍元素,使用一个栈记录所有依次递增的index,如果遇到某个元素小于前一个,则while处理栈里全部小于此元素的index,处理的方法是一次向前,然后用他的高乘以后面的长度,然后更新global变量即可。
public class Solution {
/**
* @param height: A list of integer
* @return: The area of largest rectangle in the histogram
*/
public int largestRectangleArea(int[] height) {
// write your code here
if (height == null || height.length == 0)
return 0;
Stack<Integer> stack = new Stack<Integer>();
int max = 0;
for (int i = 0; i <= height.length; i++) {
int curt = (i == height.length) ? -1 : height[i];
while (!stack.isEmpty() && curt <= height[stack.peek()]) {
int h = height[stack.pop()];
int w = stack.isEmpty() ? i : i - stack.peek() - 1;
max = Math.max(max, h * w);
}
stack.push(i);
}
return max;
}
}
3. Maximal Rectangle
这道题跟上面的题一样,只不过要预处理矩阵,取和每一列然后新建一个矩阵记录,最后历遍每一样求即可。
public class Solution {
public int maximalRectangle(char[][] matrix) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0){
return 0;
}
int[][] sum = new int[matrix.length][matrix[0].length];
for (int i = 0; i < sum.length; i++){
for (int j = 0; j < sum[i].length; j++){
sum[i][j] = matrix[i][j] - '0';
if (sum[i][j] == 1 && i > 0) {
sum[i][j] += sum[i-1][j];
}
}
}
int max = 0;
for (int i = 0; i < sum.length; i++){
Stack<Integer> s = new Stack<Integer>();
for (int j = 0; j <= sum[i].length; j++){
int currh;
if (j == sum[i].length){
currh = -1;
} else {
currh = sum[i][j];
}
//using either <= or < is ok below
while (s.size() != 0 && currh <= sum[i][s.peek()]){
int h = sum[i][s.pop()];
int w = (s.size() == 0)? j: j - (s.peek() + 1);
max = Math.max(max, h * w);
}
s.push(j);
}
}
return max;
}
}
4. Divide Two Integers
这道题有点意思,不用乘除来计算除法,方法是不停地对除数使用左移,找出最大的左移值然后累加
public class Solution {
public int divide(int dividend, int divisor) {
if(divisor==0) return Integer.MAX_VALUE;
if(divisor==-1 && dividend == Integer.MIN_VALUE)
return Integer.MAX_VALUE;
//get positive values
long pDividend = Math.abs((long)dividend);
long pDivisor = Math.abs((long)divisor);
int result = 0;
while(pDividend>=pDivisor){
//calculate number of left shifts
int numShift = 0;
while(pDividend>=(pDivisor<<numShift)){
numShift++;
}
//dividend minus the largest shifted divisor
result += 1<<(numShift-1);
pDividend -= (pDivisor<<(numShift-1));
}
if((dividend>0 && divisor>0) || (dividend<0 && divisor<0)){
return result;
}else{
return -result;
}
}
}
5. Word Search
public class Solution {
public boolean dfs(char[][] board, String word, int i, int j, int k){
int m = board.length;
int n = board[0].length;
if(i<0 || j<0 || i>=m || j>=n){
return false;
}
if(board[i][j] == word.charAt(k)){
char temp = board[i][j];
board[i][j]='#';
if(k==word.length()-1){
return true;
}else if(dfs(board, word, i-1, j, k+1)
||dfs(board, word, i+1, j, k+1)
||dfs(board, word, i, j-1, k+1)
||dfs(board, word, i, j+1, k+1)){
return true;
}
board[i][j]=temp;
}
return false;
}
public boolean exist(char[][] board, String word) {
int m = board.length;
int n = board[0].length;
boolean result = false;
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
if(dfs(board,word,i,j,0)){
result = true;
}
}
}
return result;
}
}
6. Compare Version Numbers
public class Solution {
public int compareVersion(String version1, String version2) {
String[] ver1 = version1.split("\\.");
if (ver1.length == 0){
ver1 = new String[1];
ver1[0] = version1;
}
String[] ver2 = version2.split("\\.");
if (ver2.length == 0){
ver2 = new String[1];
ver2[0] = version2;
}
int i = 0;
for (i = 0; i < ver1.length || i < ver2.length; i++){
int v1;
int v2;
if (i < ver1.length)
v1 = Integer.parseInt(ver1[i]);
else
v1 = 0;
if (i < ver2.length)
v2 = Integer.parseInt(ver2[i]);
else
v2 = 0;
if (v1 > v2)
return 1;
else if (v1 < v2)
return -1;
}
return 0;
}
}
7. Excel Sheet Column Title
public class Solution {
public String convertToTitle(int n) {
if(n <= 0){
throw new IllegalArgumentException("Input is not valid!");
}
StringBuilder sb = new StringBuilder();
while(n > 0){
n--;
char ch = (char) (n % 26 + 'A');
n /= 26;
sb.append(ch);
}
sb.reverse();
return sb.toString();
}
}
public class Solution {
public int titleToNumber(String s) {
if(s==null || s.length() == 0){
throw new IllegalArgumentException("Input is not valid!");
}
int result = 0;
int i = s.length()-1;
int t = 0;
while(i >= 0){
char curr = s.charAt(i);
result = result + (int) Math.pow(26, t) * (curr-'A'+1);
t++;
i--;
}
return result;
}
}