题目需求:

输入一个字符串,输出对应的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. RT-Thread信号量实际运用—按键点灯

    上面是魔笛开发板上 LED 和按键的 IO 分布,我们通过信号量的方法来同步按键线程和LED 线程,实现当 enter 键按下后,点亮或关闭 LED 的动作. /******************* ...

  2. using System.Diagnostics; 日志操作

    using System.Diagnostics 命名空间 包含了能够与系统进程 事件日志 和性能计数器进行交互的类 一般用于帮助诊断和调试应用程序 例如 Debug类用于帮组调试代码 Process ...

  3. mysqli常用命令

    <?php //创建连接 $mysqli=new mysqli("localhost","root","","volunte ...

  4. 【php学习】mysql数据库操作

    //建立数据库连接,数据库地址127.0.0,用户名root,密码root $dbConn = mysql_connect('127.0.0.1', 'root', 'root'); mysql_qu ...

  5. php--validate表单验证

    validate表单验证扩展规则 添加自定义检验(验证class) 获取html加入 class <input id="D_NUMBER" name="D_NUMB ...

  6. 导出证书Cer文件为Pem格式的步骤

    (1)先导出Push Services的证书,比如我们命名为“magic_cert.p12”,注意导出时会让你输入密码. (2)再导出Push Services证书的密钥(Private Key),比 ...

  7. MANIFEST.MF详解(转)

    转载自http://blog.csdn.net/zhifeiyu2008/article/details/8829637 打开Java的JAR文件我们经常可以看到文件中包含着一个META-INF目录, ...

  8. Android笔记-获取图片

     1. 图片放在sdcard中,根据路径获得: Bitmap imageBitmap = BitmapFactory.decodeFile(path) (path 是图片的路径,跟目录是/sdcard ...

  9. 判断cdn上的图片可以正常访问到

    昨天晚上cdn宕机1小时,要对上传的资检查,写了个简单的小脚本来实现上传过的资源都是正常的(其实非必须),就是练手防止生疏. arr.each do |a | res = Net::HTTP.get_ ...

  10. iOS中Objective-C与JavaScript之间相互调用的实现(实现了与Android相同的机制)转

    第三方库WebViewJavascriptBridge http://blog.csdn.net/zhaoxy_thu/article/details/22794201 Demo