Implement function atoi to convert a string to an integer.

If no valid conversion could be performed, a zero value is returned.

If the correct value is out of the range of representable values, INT_MAX(2147483647) or INT_MIN (-2147483648) is returned.

Example

"10" => 10
"-1" => -1
"123123123123123" => 2147483647
"1.0" => 1
 public class Solution {
public int myAtoi(String str) {
if (str == null || str.length() == )
return ;
str = str.trim();
if (str.length() == && (str.equals("+") || str.equals("-") || str.equals(".") || str.equals(""))) {
return ;
} boolean isPositive = true;
long current = ; for (int i = ; i < str.length(); i++) {
if (i == && str.charAt(i) == '+') {
continue;
} else if (i == && str.charAt(i) == '-') {
isPositive = false;
} else if (str.charAt(i) >= '' && str.charAt(i) <= '') {
current = current * + str.charAt(i) - '';
if (isPositive && current > Integer.MAX_VALUE)
return Integer.MAX_VALUE;
if (!isPositive && -current < Integer.MIN_VALUE)
return Integer.MIN_VALUE;
} else {
break;
}
} if (!isPositive) {
current = -current;
} return (int) current;
}
}

String to Integer的更多相关文章

  1. 【leetcode】String to Integer (atoi)

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

  2. No.008 String to Integer (atoi)

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

  3. 【LeetCode】7 & 8 - Reverse Integer & String to Integer (atoi)

    7 - Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Notic ...

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

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

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

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

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

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

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

  8. String与Integer问题

    今天分享一下关于最近面试的问题,临近春节,而我在茫茫人海中奔波,今天面试了来到了中关村科技园,挺气派的,之前也是在外面看看,今天就去了,心里有点激动,恰好,正好赶上了上班时,看见它们的努力,我感到再累 ...

  9. LeetCode--No.008 String to Integer (atoi)

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

  10. leetcode-algorithms-8 String to Integer (atoi)

    leetcode-algorithms-8 String to Integer (atoi) Implement atoi which converts a string to an integer. ...

随机推荐

  1. Ibatis中传List参数

    Ibatis中用list传参数的方式. Java代码  select count(id) from `user` where id in #[]# and status=1 . <select ...

  2. 教你一步一步实现一个Promise

    Promise我想现在大家都非常熟悉了,主要作用就是解决异步回调问题,这里简单介绍下. Promise规范是CommonJS规范之一,而Promise规范又分了好多种,比如 Promises/A.Pr ...

  3. Luncene 学习入门

    Lucene是apache组织的一个用java实现全文搜索引擎的开源项目. 其功能非常的强大,api也很简单.总得来说用Lucene来进行建立 和搜索和操作数据库是差不多的(有点像),Document ...

  4. myeclipse-建立webservice服务端和客户端

    一.建立webservice服务端: 1.新建一个web service project,名称为webservice_server截图如下,点击finish. 2.选择工程,点击右键,选择new-&g ...

  5. 【HTTP劫持和DNS劫持】腾讯的实际业务分析

        简单介绍一下HTTP劫持和DNS劫持的概念,也就是运营商通过某些方式篡改了用户正常访问的网页,插入广告或者其他一些杂七杂八的东西.       首先对运营商的劫持行为做一些分析,他们的目的无非 ...

  6. NOI题库 09:图像旋转翻转变换

    NOI题库开始的题,也是略水,当然也是大水,所以彼此彼此 09:图像旋转翻转变换 总时间限制: 1000ms 内存限制: 65536kB 描述 给定m行n列的图像各像素点灰度值,对其依次进行一系列操作 ...

  7. SpringMVC 配置log4j

    log4j.properties相关 http://www.cnblogs.com/ITEagle/archive/2010/04/23/1718365.html http://www.cnblogs ...

  8. POJ2531Network Saboteur(DFS+剪枝)

    Network Saboteur Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10391   Accepted: 4990 ...

  9. 深入解析MySQL分区(Partition)功能

    自5.1开始对分区(Partition)有支持 = 水平分区(根据列属性按行分)= 举个简单例子:一个包含十年发票记录的表可以被分区为十个不同的分区,每个分区包含的是其中一年的记录. === 水平分区 ...

  10. resharper安装后,一不小心点错了(选择了object browser)

    打开Resharper,选择Options,然后选择Tools中的External Sources,你的情况是选择了Navigation to Object Brower这一项了,换成第一个Defau ...