题目需求:

输入一个字符串,输出对应的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. 20145235 《Java程序设计》第9周学习总结

    第十六章 整合数据库 JDBC Java语言访问数据库的一种规范,是一套API. JDBC (Java Database Connectivity) API,即Java数据库编程接口,是一组标准的Ja ...

  2. SQL查询语句中的 limit offset(转 )

    经常用到在数据库中查询中间几条数据的需求 比如下面的sql语句: ① selete * from testtable limit 2,1; ② selete * from testtable limi ...

  3. [收藏]ASP.NET MVC管道详述

    ASP.NET MVC从诞生到现在已经好几个年头了,这个框架提供一种全新的开发模式,更符合web开发本质.你可以很好的使用以及个性化和扩展这个框架,但这需要你对它有足够的了解.这篇文章主要从整体角度总 ...

  4. Mongo对内嵌文档的CRUD

    { "_id" : ObjectId("5706032acd0a6194868cf53e"), "list" : { "age&q ...

  5. Linux环境下配置eclipse,以及创建maven工程

    一:maven的安装 1.安装配置maven环境变量 2.验证 二:eclipse的安装 3.解压配置eclipse 4.启动eclipse,必须在虚拟机的eclipse下启动 5.结果 三:修改配置 ...

  6. Mixing Delphi and C++(相互调用)

    Mixing Delphi and C++ You have a TStringList and <algorithm>. What can you do? Quite a lot, ac ...

  7. .NET对象与Windows句柄(三):句柄泄露实例分析

    在上篇文章.NET对象与Windows句柄(二):句柄分类和.NET句柄泄露的例子中,我们有一个句柄泄露的例子.例子中多次创建和Dispose了DataReceiver和DataAnalyzer对象, ...

  8. java的Random

    首先,Point类 public class Point { int x, y; public Point(int x, int y) { this.x = x; this.y = y; } bool ...

  9. 网页中的超链接<a>标签

    格式: <a href="目标网址" title="鼠标滑过显示的文本">链接显示的文本</a> 注意:为文本加入<a>标签 ...

  10. 在magento中如何回复客户的评论

    magento — 在magento中如何回复客户的评论 发表于 2012 年 8 月 18 日 agento本身是不带 回复评论的功能的,现成的扩展(无论免费的还是商业的)也没找到,那就自己写一个吧 ...