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. GridView获取CheckBox的值及所在列的ID

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

  2. Configure the Windows Firewall to Allow SQL Server Access

    参考微软链接: https://msdn.microsoft.com/zh-tw/library/cc646023.aspx

  3. Xcode6中如何去掉默认的Main.storyboard

    xcode 6取消了 Empty Application 模板来创建一个工程,创建出来的有工程多了Main.storyboard,默认加载Main.storyboard,但是有很多人还想用代码来实现U ...

  4. HttpContext详解

    HttpContext 类:封装有关个别 HTTP 请求的所有 HTTP 特定的信息. 在处理请求执行链的各个阶段中,会有一个对象在各个对象之间进行传递,也即会保存 请求的上下文信息,这个对象就是Ht ...

  5. eclipse搭建servlet项目

    1.创建web项目 2.勾选Generate web.xml 3.创建Class文件并实现Servlet接口 当搜索Servlet接口时,如果未发现接口则Add library→选择tomcat版本至 ...

  6. android操作线程各种方法解析

    (一)刚开始学习android的时候我是这么写的 new Thread( new Runnable() { public void run() { myView.invalidate(); } }). ...

  7. Sunny-Code 最终版总结会议

    设想和目标 我们的软件要解决什么问题?是否定义得很清楚? 我们打算做一款集成小蝴蝶功能.Ip快速修改功能.WiFi共享功能的一款软件.目的是为了解决晚上小蝴蝶断线重连问题.还有新生不会修改IP问题. ...

  8. 前端二:CSS

    CSS: 一:介绍:学名层叠样式表(Cading Style Sheets)是一种用来表现HTML或者XML等文件的样式的计算机语言.让HTML和XML看起来更加美观. 语法:<style> ...

  9. Localization要从第一天开始计划

    最近E3,微软说可以在任何region玩任何语言的游戏了.换一个语言么,听起来没有那么复杂,其实操作起来还得是从软件工程初期就好好计划. Windows在很长一段时间,你安装完了,就不能换语言了.大学 ...

  10. Outline of Apache Jena Notes

    1 description 这篇是语义网应用框架Apache Jena学习记录的索引. 初始动机见Apache Jena - A Bootstrap 2 Content 内容组织基本上遵循Jena首页 ...