public int myAtoi(String s) {
s=s.trim();
if (s.length()==0) return 0;
if (!Character.isDigit(s.charAt(0))&&s.charAt(0)!='+'&&s.charAt(0)!='-') return 0;
boolean flag=true;
if (s.charAt(0)=='+') {
s=s.substring(1);
}
else if (s.charAt(0)=='-'){
s=s.substring(1);
flag=false;
}
long ans=0;
for (int i=0;i<s.length();i++){
if (!Character.isDigit(s.charAt(i))&&flag) return (int) ans;
if (!Character.isDigit(s.charAt(i))&&!flag) return (int) -ans;
int c=Integer.parseInt(s.charAt(i)+"");
ans=ans*10+c;
if (flag&&ans>=Integer.MAX_VALUE) return Integer.MAX_VALUE;
if (!flag&&-ans<=Integer.MIN_VALUE) return Integer.MIN_VALUE;
}
return flag?(int)ans:(int)-ans;
}
2、回文数
public boolean isPalindrome(int x) {
String s1=String.valueOf(x);
StringBuilder s2=new StringBuilder();
for (int i=s1.length()-1;i>=0;i--){
s2.append(s1.charAt(i));
}
return s1.equals(s2.toString());
}
public boolean isPalindrome2(int x) {
if (x<0||(x%10==0&&x!=0)) return false;
int revertNum=0;
while (x>revertNum){
revertNum=revertNum*10+x%10;
x/=10;
}
return x==revertNum||x==revertNum/10;
}
3、盛最多水的容器
public int maxArea(int[] height) {
int ans=0,l=0,r=height.length-1;
while (r>l){
int area=Math.min(height[l],height[r])*(r-l);
ans=Math.max(area,ans);
if (height[r]>height[l]) ++l;
else ++r;
}
return ans;
}