Implement atoi to convert a string to an integer.

 public class Solution {
public int myAtoi(String s) {
if (s == null || s.length() < 1) {
return 0;
}
int i = 0;
while (i < s.length() && s.charAt(i) == ' ') {
i++;
}
if (i == s.length()) {
return 0;
}
boolean isN = false;
if (s.charAt(i) == '-') {
isN = true;
i++;
} else if (s.charAt(i) == '+') {
i++;
}
double r = 0;
for (; i < s.length(); i++) {
if (s.charAt(i) < '0' || s.charAt(i) > '9') {
break;
}
r = r * 10 + (s.charAt(i) - '0');
}
if (isN) {
r = -r;
if (r < Integer.MIN_VALUE) {
return Integer.MIN_VALUE;
}
} else if (r > Integer.MAX_VALUE) {
return Integer.MAX_VALUE;
}
return (int)r;
}
}

[LeetCode] 8. String to Integer (atoi)的更多相关文章

  1. Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)

    Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...

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

  3. 【leetcode】String to Integer (atoi)

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

  4. [leetcode] 8. String to Integer (atoi) (Medium)

    实现字符串转整形数字 遵循几个规则: 1. 函数首先丢弃尽可能多的空格字符,直到找到第一个非空格字符. 2. 此时取初始加号或减号. 3. 后面跟着尽可能多的数字,并将它们解释为一个数值. 4. 字符 ...

  5. Leetcode 8. String to Integer (atoi)(模拟题,水)

    8. String to Integer (atoi) Medium Implement atoi which converts a string to an integer. The functio ...

  6. [LeetCode][Python]String to Integer (atoi)

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/string- ...

  7. 【LeetCode】String to Integer (atoi) 解题报告

    这道题在LeetCode OJ上难道属于Easy.可是通过率却比較低,究其原因是须要考虑的情况比較低,非常少有人一遍过吧. [题目] Implement atoi to convert a strin ...

  8. LeetCode 8. String to Integer (atoi) (字符串到整数)

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  9. [LeetCode] 8. String to Integer (atoi) 字符串转为整数

    Implement atoi which converts a string to an integer. The function first discards as many whitespace ...

  10. LeetCode 7 -- String to Integer (atoi)

    Implement atoi to convert a string to an integer. 转换很简单,唯一的难点在于需要开率各种输入情况,例如空字符串,含有空格,字母等等. 另外需在写的时候 ...

随机推荐

  1. c语言中gets ,getschar 和fgets 的用法及三者之间的差别,还有scanf

    ①gets [1]函数:gets(字符指针) [2]头文件:stdio.h(c中),c++不需包含此头文件 [3]原型:char*gets(char*buffer); [4]功能:从stdin流中读取 ...

  2. 三种renderman规范引擎的dice对比

    次表面做的有些烦躁,既然如此,索性先记一下前一阵比较的PIXIE.3delight.prman的dice方式. 研究过reyes的人都知道dice,简而言之,就是为了生成高质量高精度的图片(电影CG) ...

  3. GridView获取CheckBox的值及所在列的ID

    //根据GridView的列数进行循环 ; i < GridView1.Rows.Count; i++) { //检查是否有ID为CheckBox1的CheckBox控件,如果有就赋值给Chec ...

  4. amazon o2 - reverse second half linked list

    public ListNode reverseBetween(ListNode head, int m, int n) { if(head==null) return head; ListNode s ...

  5. CSS教程:div垂直居中的N种方法[转]

    在说到这个问题的时候,也许有人会问CSS中不是有vertical-align属性来设置垂直居中的吗?即使是某些浏览器不支持我只需做少许的CSS Hack技术就可以啊!所以在这里我还要啰嗦两句,CSS中 ...

  6. Linux自己安装redis扩展

    注意:目录的权限   chomd 777 -R 注意:下面涉及的目录只是我自己安装的目录,要找自己对应的目录文件 1.安装redis 下载:https://github.com/nicolasff/p ...

  7. 2.4G无线射频通信模块nRF24L01+开发笔记(基于MSP430RF6989与STM32f0308)(1.(2)有错误,详见更正)

    根据网上的nRF24L01+例程和TI提供的MSP430RF6989的硬件SPI总线例程编写程序,对硬件MSP-EXP430RF6989 Launch Pad+nRF24L01P射频模块(淘宝购买)进 ...

  8. (转)实例简述Spring AOP之间对AspectJ语法的支持(转)

    Spring的AOP可以通过对@AspectJ注解的支持和在XML中配置来实现,本文通过实例简述如何在Spring中使用AspectJ.一:使用AspectJ注解:1,启用对AspectJ的支持:通过 ...

  9. php-长文章分页函数

    <?php function ff_page($content,$page) { global $expert_id; $PageLength = 2000; //每页字数 $CLength = ...

  10. [ JS 进阶 ] 基本类型 引用类型 简单赋值 对象引用

    ECMAScirpt 变量有两种不同的数据类型:基本类型,引用类型.也有其他的叫法,比如原始类型和对象类型,拥有方法的类型和不能拥有方法的类型,还可以分为可变类型和不可变类型,其实这些叫法都是依据这两 ...