• API
• FAQ
• Tools
• Archive
daily pastebin goal
23%
SHARE
TWEET

# Untitled

a guest Dec 19th, 2018 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. import java.math.BigInteger;
2. class Solution {
3.     public int myAtoi(String str) {
4.         StringBuilder sb = new StringBuilder();
5.         int f=0;
6.
7.         for(f=0;f<str.length();f++){
8.             if(str.charAt(f)==' '){
9.                 continue;
10.             }else{
11.                 break;
12.             }
13.         }
14.
15.         if(f<str.length() && (str.charAt(f)=='+' || str.charAt(f)=='-')){
16.             sb.append(str.charAt(f));
17.             f++;
18.         }
19.
20.         for(int i=f;i<str.length();i++){
21.             if((str.charAt(i)>=48 && str.charAt(i)<=57)){
22.                 sb.append(str.charAt(i));
23.                 continue;
24.             }else{
25.                 break;
26.             }
27.         }
28.
29.         String ret = sb.toString();
30.         if(ret.length()==0 || (ret.length()==1 && (ret.charAt(0)=='+' || ret.charAt(0)=='-'))) {
31.             return 0;
32.         }
33.         BigInteger res = new BigInteger(ret);
34.         if(res.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) > 0) {
35.             return Integer.MAX_VALUE;
36.         }
37.         if(res.compareTo(BigInteger.valueOf(Integer.MIN_VALUE)) < 0) {
38.             return Integer.MIN_VALUE;
39.         }
40.         return res.intValue();
41.
42.     }
43. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top