题目需求:

输入一个字符串,输出对应的int值

特殊处理:

输入: null  输出:0

输入: "a122"  输出:0

输入: "   1233"  输出:1233

输入: "  -123"  输出:-123

输入: "+123"  输出:123

输入: "  123a233"  输出:123

输入: "-123 a 34" 输出:-123

输入: "1232334344556566"  输出:int类型的最大值

输入: "-123444554558"  输出:int类型的最小值

思路:

1、处理正号和负号

2、处理空格

3、处理数据的溢出

4、处理数据中间出现非数字字符

代码:

    public static int myAtoi(String str){
if(str == null || str.equals("")){
return 0;
}
char[] chs = str.toCharArray();
long num = 0;
boolean flag1 = true;
int flag = 1;
for(int i=0; i<chs.length; i++){
//处理正负符号
if(flag1 && chs[i] == '+'){
flag1 = false;
continue;
}
if(flag1 && chs[i] == '-'){
flag = -1;
flag1 = false;
continue;
}
//处理空格
if(flag1 && chs[i] == ' '){
continue;
}
if(!(chs[i] >= '0' && chs[i] <= '9')){
//判断是否溢出
long num1 = num*flag;
if( num1 >= Integer.MAX_VALUE){
return Integer.MAX_VALUE;
}else{
if(num1 <= Integer.MIN_VALUE){
return Integer.MIN_VALUE;
}else{
return (int)num1;
}
}
}else{
flag1 = false;
num = num*10 + chs[i]-'0';
long num1 = num*flag;
if( num1 >= Integer.MAX_VALUE){
return Integer.MAX_VALUE;
}else{
if(num1 <= Integer.MIN_VALUE){
return Integer.MIN_VALUE;
}else{
continue;
}
}
}
}
long num1 = num*flag;
if( num1 >= Integer.MAX_VALUE){
return Integer.MAX_VALUE;
}else{
if(num1 <= Integer.MIN_VALUE){
return Integer.MIN_VALUE;
}else{
return (int)num1;
}
}
}

leetcode8 String to Integer (atoi)的更多相关文章

  1. LeetCode----8. String to Integer (atoi)(Java)

    package myAtoi8; /* * Implement atoi to convert a string to an integer. Hint: Carefully consider all ...

  2. Leetcode8.String to Integer (atoi)字符串转整数(atoi)

    实现 atoi,将字符串转为整数. 该函数首先根据需要丢弃任意多的空格字符,直到找到第一个非空格字符为止.如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连续的数字组合起来,这部分字 ...

  3. LeetCode 8. 字符串转换整数 (atoi)(String to Integer (atoi))

    8. 字符串转换整数 (atoi) 8. String to Integer (atoi) 题目描述 LeetCode LeetCode8. String to Integer (atoi)中等 Ja ...

  4. 【leetcode】String to Integer (atoi)

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

  5. No.008 String to Integer (atoi)

    8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...

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

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

  7. 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 ...

  8. String to Integer (atoi) - 字符串转为整形,atoi 函数(Java )

    String to Integer (atoi) Implement atoi to convert a string to an integer. [函数说明]atoi() 函数会扫描 str 字符 ...

  9. Kotlin实现LeetCode算法题之String to Integer (atoi)

    题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 class ...

随机推荐

  1. laravel 数据填充

    编写填充器 php artisan make:seeder UserTableSeeder 修改Laravel安装时自带的DatabaseSeeder类,添加一个数据库插入语句到run方法: < ...

  2. writing concurrent programs

    Computer Systems A Programmer's Perspective Second Edition To this point in our study of computer sy ...

  3. ifarm 子 父页面方法如何互调

    1.iframe子页面调用父页面js函数 子页面调用父页面函数只需要写上window.praent就可以了.比如调用a()函数,就写成: 代码如下: window.parent.a(); 子页面取父页 ...

  4. 基于java工程开发RMI服务端

    ServiceRegist.java import java.rmi.Remote; import java.rmi.RemoteException; public interface Service ...

  5. foreach遍历 < 创建表 >练习题

    原表如下: 效果图如下: <table border="1" width="500" height="260"><tr&g ...

  6. MVC HTML辅助类常用方法记录

    (1)@Html.DisplayNameFor(model => model.Title)是显示列名, (2)@Html.DisplayFor(modelItem => item.Titl ...

  7. NSArray和NSDictionary添加空对象,以及nil和Nil和NULL和NSNull

    因为在NSArray和NSDictionary中nil中有特殊的含义(表示列表结束),所以不能在集合中放入nil值.如要确实需要存储一个表示“什么都没有”的值,可以使用NSNull类. NSNull只 ...

  8. svn利用TortoiseSVN忽略文件或文件夹

    忽略已经版本控制的文件 如果你不小心添加了一些应该被忽略的文件,你如何将它们从版本控制中去除而不会丢失它们?或许你有 自己的IDE配置文件,不是项目的一部分,但将会花费很多时间使之按照自己的方式工作. ...

  9. php mysql连接例子

    <?PHP @$conn = mysql_connect("127.0.0.1","root",""); //返回false或reso ...

  10. HBASE架构解析(二)

    http://www.blogjava.net/DLevin/archive/2015/08/22/426950.html HBase读的实现 通过前文的描述,我们知道在HBase写时,相同Cell( ...