Guest User

Untitled

a guest
Dec 19th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  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. }
Add Comment
Please, Sign In to add comment