package myAtoi8;
/*
* Implement atoi to convert a string to an integer.
Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.
Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.
Update (2015-02-10):
The signature of the C++ function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button to reset your code definition.
*/ public class Solution {
public static int myAtoi(String str) {
StringBuilder sb=new StringBuilder();
int flag=0;
if(str.length()==0)
return 0;
else{
for(int i=0;i<str.length();i++){
if(str.charAt(i)<'0'||str.charAt(i)>'9')
if(str.charAt(i)=='0'||str.charAt(i)==' ')
if (sb.length()==0)
continue;
else
break;
else if (str.charAt(i)=='-')
if (flag==0){
flag=-1;
sb.append('0');
}
else
break;
else if (str.charAt(i)=='+')
if (flag==0){
flag=1;
sb.append('0');
}
else
break;
else
break;
else
sb.append(str.charAt(i));
}
}
if(sb.length()==0)
return 0;
else if(sb.length()>12)
str=sb.substring(sb.length()-12);
else
str=sb.toString();
if (flag==-1)
str="-"+str;
long result=Long.parseLong(str);
System.out.println(result);
if (result>2147483647)
return 2147483647;
else if (result<-2147483648)
return -2147483648;
else
return (int)result; }
public static void main(String[] args){
String str1="";//
String str2="+";//
String str3=" +45555";//
String str4=" -45555";//-45555
String str5="2147483647";//
String str6="2147483648";//
String str7="+-2";//
String str8="-+2";//
String str9="++2";//
String str10="-2147483648";//-2147483648
String str11=" - 321";//
String str12=" -11919730356x";//-2147483648
String str13="1234567890123456789012345678901234567890";//
System.out.println(myAtoi(str1));
System.out.println(myAtoi(str2));
System.out.println(myAtoi(str3));
System.out.println(myAtoi(str4));
System.out.println(myAtoi(str5));
System.out.println(myAtoi(str6));
System.out.println(myAtoi(str7));
System.out.println(myAtoi(str8));
System.out.println(myAtoi(str9));
System.out.println(myAtoi(str10));
System.out.println(myAtoi(str11));
System.out.println(myAtoi(str12));
System.out.println(myAtoi(str13)); }
}

LeetCode----8. String to Integer (atoi)(Java)的更多相关文章

  1. Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)

    Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...

  2. LeetCode【8】. String to Integer (atoi) --java实现

    String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...

  3. leetcode第八题 String to Integer (atoi) (java)

    String to Integer (atoi) time=272ms   accepted 需考虑各种可能出现的情况 public class Solution { public int atoi( ...

  4. leetcode day6 -- String to Integer (atoi) &amp;&amp; Best Time to Buy and Sell Stock I II III

    1.  String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully con ...

  5. 【leetcode】String to Integer (atoi)

    String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...

  6. [leetcode] 8. String to Integer (atoi) (Medium)

    实现字符串转整形数字 遵循几个规则: 1. 函数首先丢弃尽可能多的空格字符,直到找到第一个非空格字符. 2. 此时取初始加号或减号. 3. 后面跟着尽可能多的数字,并将它们解释为一个数值. 4. 字符 ...

  7. Leetcode 8. String to Integer (atoi)(模拟题,水)

    8. String to Integer (atoi) Medium Implement atoi which converts a string to an integer. The functio ...

  8. [LeetCode][Python]String to Integer (atoi)

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/string- ...

  9. 【JAVA、C++】 LeetCode 008 String to Integer (atoi)

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  10. Java [leetcode 8] String to Integer (atoi)

    问题描述: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ...

随机推荐

  1. 内网网段划分ciso交换机配置

    内网3750交换机配置: vlan 192 192.168.101.0/24 端口: 1--8vlan 10 10.10.10.0/24 端口: 9--16vlan 172 172.16.172.2/ ...

  2. SBT Assembly - Deduplicate error & Exclude error

    sbt assembly java.lang.RuntimeException: deduplicate: different file contents found in the following ...

  3. JS简单的图片左右滚动

    <div id="scroll" style="overflow:hidden;width:757px;"> <table cellpaddi ...

  4. linux笔记七---------管道

    smarty的变量调节器就是linux的管道 管道:前者的输出是后者的输入 {$name|upper}   通过调节器使得名字变为大写输出 {$name|lower} linux的管道: ls –al ...

  5. 模拟状态为active的日志损坏的数据恢复实验(不完全恢复)

    1查看当前日志状态 首先不完全恢复是会丢失数据的,由此在当前打开的数据中我们创建一些测试数据,用来验证当我们进行完不完全恢复后该数据是否还存在. 2模拟删除CURRENT状态的日志 3启动数据验证错误 ...

  6. zju(8)串口通信实验

    1.实验目的 1.学习和掌握linux下串口的操作方法以及应用程序的编写: 二.实验内容 1.编写EduKit-IV实验箱Linux操作系统下串口的应用程序,运行时只需要将串口线的一端连接到开发板的c ...

  7. BizTalk开发系列(十四) XML空白字符(WhiteSpace)

    最近在做一个BizTalk项目,对XML文件的处理很复杂.本来是想找有没有方法可以一次性去除XML文件中节点和属性的值的空格.但是找了很久没有看到相关的方法.如果有知道该方法的麻烦跟我讲一下:cbcy ...

  8. [转]线性插值&双线性插值&三线性插值

    转自:http://www.cnblogs.com/yingying0907/archive/2012/11/21/2780092.html 內插是数学领域数值分析中的通过已知的离散数据求未知数据的过 ...

  9. Python开发【第七章】:Python异常处理

    一.异常处理 1.异常基础 在编程过程中为了增加友好性,在程序出现bug时一般不会将错误信息显示给用户,而是现实一个提示的页面,通俗来说就是不让用户看见大黄页!!! #异常处理 list = [&qu ...

  10. dubbo源码分析3-service bean的创建与发布

    dubbo源码分析1-reference bean创建 dubbo源码分析2-reference bean发起服务方法调用 dubbo源码分析3-service bean的创建与发布 dubbo源码分 ...