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. 从零开始设计SOA框架(三):请求参数的加密方式

    第二章中说明请求参数有哪些,主要是公共参数和业务参数,服务端需要对参数进行效验,已验证请求参数的合法性 参数效验前先解释下以下参数: 1.参数键值对:包括公共参数.业务参数      1.公共参数:按 ...

  2. 【 CodeForces 604A】B - 特别水的题2-Uncowed Forces

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=102271#problem/B Description Kevin Sun has jus ...

  3. 重写UIPageControl实现自定义按钮

    有时候UIPageControl需要用到白色的背景, 那么会导致上面的点按钮看不见或不清楚,我们可以通过继承该类重写函数来更换点按钮的图片现实.实现思路如下.新建类继承UIPageControl :  ...

  4. MyEclipse护眼模式、字体大小的调整

    1.Eclipse改变背景颜色 Windows menu --> Preference General -> Editors -> Text Editors(click),  在底部 ...

  5. poj3207 2-SAT入门

    一开始题意没读懂 = = 题意:比如说对于表盘上a到b.c到d都要连边,这两个边不能交叉.这两个边要么都在圆内要么都在圆外,而且可以是曲线= = 比如这种情况:(Reference:http://bl ...

  6. groovy-保留字

    groovy的保留字: abstractasassertbooleanbreakbytecasecatchcharclassconstcontinuedefdefaultdodoubleelseenu ...

  7. 异步httpCilent框架post提交到服务器

    1 导入AsyncHttPclient框架包下的类: 2在主方法写上这代码即可实现post请求:

  8. safeNet

    把那4个dll丢到C:\Windows\SysWOW64里去重启IIS,再测试

  9. polling 和 long polling 工作原理

    polling & long polling 参考:http://stackoverflow.com/questions/11077857/what-are-long-polling-webs ...

  10. hibernate的pojo和xml文件