[LeetCode]-algorithms-String to Integer (atoi)
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)的更多相关文章
- Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)
Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...
- leetcode day6 -- String to Integer (atoi) && 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 ...
- 【leetcode】String to Integer (atoi)
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- [leetcode] 8. String to Integer (atoi) (Medium)
实现字符串转整形数字 遵循几个规则: 1. 函数首先丢弃尽可能多的空格字符,直到找到第一个非空格字符. 2. 此时取初始加号或减号. 3. 后面跟着尽可能多的数字,并将它们解释为一个数值. 4. 字符 ...
- Leetcode 8. String to Integer (atoi)(模拟题,水)
8. String to Integer (atoi) Medium Implement atoi which converts a string to an integer. The functio ...
- [LeetCode][Python]String to Integer (atoi)
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/string- ...
- 【LeetCode】String to Integer (atoi) 解题报告
这道题在LeetCode OJ上难道属于Easy.可是通过率却比較低,究其原因是须要考虑的情况比較低,非常少有人一遍过吧. [题目] Implement atoi to convert a strin ...
- LeetCode 8. String to Integer (atoi) (字符串到整数)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- [LeetCode] 8. String to Integer (atoi) 字符串转为整数
Implement atoi which converts a string to an integer. The function first discards as many whitespace ...
- LeetCode 7 -- String to Integer (atoi)
Implement atoi to convert a string to an integer. 转换很简单,唯一的难点在于需要开率各种输入情况,例如空字符串,含有空格,字母等等. 另外需在写的时候 ...
随机推荐
- P1142轰炸
这是uva上的一道模拟题. 首先给出n(n<=700)个点的坐标(坐标在1*10^9)之内,询问走直线可以经过的点数.一开始我想到了一个类似于桶排序的方法来存坐标,但是要注意数组大小啊!第二次想 ...
- npm学习(四)之如何安装全局包、更新全局安装的包、卸载全局安装的包
如何安装全局包 有两种方式用来安装 npm 包:本地安装和全局安装.选用哪种方式来安装,取决于你如何使用这个包. 如果你想将其作为一个命令行工具,那么你应该将其安装到全局.这种安装方式后可以让你在任何 ...
- php-fpm内存泄漏问题排查
生产环境内存泄漏问题排查,以下是排查思路 生产环境上有严重的内存溢出问题(红色框所示,正常值应为是 20M 左右)同时系统有 Core Dump 文件产生排查过程中还发现一个现象,如果关闭 OPc ...
- 自己对GIS的思考
这只是我自己的理解,谈不上对整个行业的理解,只能从自己的角度谈谈GIS,谈谈爱和恨. 现在在武汉的一所所谓的全国GIS数一数二的学校里面读硕士,从高中开始我就很喜欢地理学科,大学选择了地球信息科技这个 ...
- hier - 文件系统描述
DESCRIPTION 描述 一个典型的Linux系统具有以下几个目录: / 根目录,是所有目录树开始的地方. /bin 此目录下包括了单用户方式及系统启动或修复所用到的所有执行程序. /boot 包 ...
- 如何在 Visual C# 中执行基本的文件 I/O
演示文件 I/O 操作 本文中的示例讲述基本的文件 I/O 操作.“分步示例”部分说明如何创建一个演示下列六种文件 I/O 操作的示例程序: 注意:如果要直接使用下列示例代码,请注意下列事项: 必须包 ...
- 微信支付-无法识别qrcode生成的二维码图片
1.开始使用 table方式,但是还是无法识别二维码 http://www.cnblogs.com/staticed/p/8549316.html var code_url = data.code_ ...
- Laravel5.5去除URL中的index.php生成优雅链接
在使用Apache情况下: Laravel 框架通过 public/.htaccess 文件来让网址中不需要 index.php.如果你的服务器是使用 Apache ,请确认是否有开启 mod_rew ...
- 【洛谷P1919】A*B Problem升级版
题目大意:rt 题解:将长度为 N 的大整数看作是一个 N-1 次的多项式,利用 FFT 计算多项式的卷积即可. 代码如下 #include <bits/stdc++.h> using n ...
- Python修炼之路-异常
异常处理 在程序出现bug时一般不会将错误信息直接显示给用户,而是可以自定义显示内容或处理. 常见异常 AttributeError # 试图访问一个对象没有的树形,比如foo.x,但是foo没有属性 ...