Implement atoi to convert a string to an integer.

Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.

Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.

需求:字符串转成数字,需要识别正负号

""
=>0
-1
=>-1
+-1
=>0
1234567890123456789012345678901234567890
=>0
"2147483648"
=>2147483647

public int myAtoi(String str) {
str = str.trim();
if(null==str || "".equals(str))
return 0;
char flag = '+';
int i = 0;
if(str.charAt(0)=='+'){
flag = '+';
i++;
}else if(str.charAt(0)=='-'){
flag = '-';
i++;
}
double result = 0.0;
if(str.length()>12)
return 0;
while(str.length()>i && str.charAt(i)>='0' && str.charAt(i)<='9'){
result = result * 10 + (str.charAt(i)-'0');
i++;
}
if(flag=='-')
result = -result;
if(result>Integer.MAX_VALUE)
result = Integer.MAX_VALUE;
if(result<Integer.MIN_VALUE)
result = Integer.MIN_VALUE;
return (int)result;
}

[LeetCode]-algorithms-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. Openresrt最佳案例

    第1篇:Nginx介绍 Nginx是一个高性能的Web 服务器,同时是一个高效的反向代理服务器,它还是一个IMAP/POP3/SMTP 代理服务器. 由于Nginx采用的是事件驱动的架构,能够处理并发 ...

  2. Swoole开启守护进程后如何关闭

    查找相应端口号对应的PID(以我的为例,我的是9501端口) netstat -apn | 清除这个进程 启动客户端这时就会报错连不上了,证明服务已关

  3. 什么是 MicroPython ?它有什么优势?

    MicroPython 和 Python 都是一类 Python 的动态语言解释器. 若是在系统里安装了 Python 解释器,例如在 Windows 或者 Linux 命令行输入 python,进入 ...

  4. 085、如何快速部署 Prometheus (2019-05-07 周二)

    参考https://www.cnblogs.com/CloudMan6/p/7724576.html   部署环境:       两台 Docker Host 10.12.31.211 10.12.3 ...

  5. 定义一个Book类,有书名,价格,作者等信息。定义相应的方法来改变这些属性的值。定义一个方法来显示Book的所有信息。

    package com.fs.test; public class Test { public static void main(String[] args) { // 先声明后赋值 book b;/ ...

  6. Js和Jquery实现ajax长轮询

    众所周知,HTTP协议是无状态的,所以一次的请求都是一个单独的事件,和前后都没有联系.所以我们在解决网页实时聊天时就遇到一个问题,如何保证与服务器的长时间联系,从而源源不段地获取信息. 一直以来的方式 ...

  7. zencart批量表上传后 标题显示为网址 批量修改标题状态 SEO三要素

    zencart批量表上传后 标题显示为网址,原因是导入批量表时,产品标题对应状态被重置为0导致的 批量修改标题状态 ', metatags_products_name_status ', metata ...

  8. squid之------安装与基本配置

    1.rpm安装squid yum -y install squid 2.squid主要组成部分 服务名:squid 主程序:/usr/sbin/squid 配置目录:/etc/squid 主配置文件: ...

  9. ax2+bx+c=0的根的算法

    每日一练作业 写一个函数,接受三个整数a, b, c,计算ax2+bx+c=0 的根. 另外,在计算时应当判断 b2 - 4ac 是否大于0. 我们什么都没有,唯一的本钱就是青春.梦想让我与众不同,奋 ...

  10. LAMP 系统性能调优之内核调优措施

    LAMP 系统性能调优之内核调优措施 2011-03-18 11:21 Sean A. Walberg 网络转载 字号:T | T 在对系统的 Apache.PHP 和 MySQL 组件进行调优之前, ...