9.回文数
class Solution {
public boolean isPalindrome(int x) {
if(x < 0)
return false;
int y = 0, t, n = x;
while(x > 0){
t = x % 10;
y = y*10 + t;
x = x / 10;
}
return n == y;
}
}
11. 盛最多的水
解法一:
class Solution {
//遍历所有的组合,找出最大值
public int maxArea(int[] height) {
int n = height.length;
int max = 0;
for(int i = 0; i < n-1; i ++){
for(int j = 1; j < n; j ++){
int width = j - i;
int length = height[i] < height[j] ? height[i] : height[j];
if(max < width*length)
max = width*length;
}
}
return max;
}
}
解法二:
class Solution {
//从容器的左右两端开始计算,每次都将矮的那边前进一格
public int maxArea(int[] height) {
int i = 0, j = height.length - 1;
int width, length;
int s = 0;
while(i < j){
width = j - i;
length = height[i] < height[j] ? height[i] : height[j];
if(s < width*length)
s = width*length;
if(height[i] < height[j])
i ++;
else
j --;
}
return s;
}
}