[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. 转换很简单,唯一的难点在于需要开率各种输入情况,例如空字符串,含有空格,字母等等. 另外需在写的时候 ...
随机推荐
- python——列表方法
L.append():追加一个元素到列表末尾 L = [] L.append('boy') L.insert() :将一个元素插入到指定位置 L.insert(1, 'girl') L.extend( ...
- uoj #242【UR #16】破坏蛋糕
uoj 考虑把那最后一条直线拎出来,并且旋转到和\(y\)轴平行(其他直线同时一起旋转),然后它和其他直线相交形成\(n+1\)个区间,现在要知道这些区间是否处在一个面积有限的区域 可以发现一段在有限 ...
- multipart/form-data提交
pip install requests-toolbelt from requests_toolbelt import MultipartEncoder import requests m = Mul ...
- ArrayList实现原理分析
ArrayList使用的存储的数据结构 ArrayList的初始化 ArrayList是如何动态增长 ArrayList如何实现元素的移除 ArrayList小结 ArrayList是我们经常使用的一 ...
- 从零开始学MySQL(二)
鉴于上节篇幅以安装为主,因此对于调用mysql所需要使用的“命令”只是略微提及.随之而来就会带给读者诸多不解了,因为你会思考,这串长长的字符到底有什么特殊的含义呢?聪明的你可能早就抱着好奇心去“摆渡” ...
- Visual Studio Code python 代码快速自动提示
1.file --> setting->设置 搜索 python 或者auto_complete setting.json { "explorer.confirmDelete&q ...
- 交叉工具链和makefile
交叉工具链: arm-linux-gcc:交叉编译器 arm-linux-ld:交叉连接器 arm-linux-readelf:交叉ELF文件工具 arm-linux-objdump:交叉反汇编器 a ...
- 清北学堂清华大学钟皓曦神仙讲课day3摘要
---恢复内容开始--- 今天全是DP awsl,真的好难 先从斐波那契开始: dp:满足有一个状态边界条件(f[0]=0,f[1]=1) 边界条件:不需要计算其他状态的值而可以直接得出的状态或者最底 ...
- Python-Django的windows环境
下载安装python2.7 : 最好是安装win32的,64bit的很多的lib都不支持.python-2.7.3 http://python.org/getit/releases/2.7.3/ 下载 ...
- C++ std::vector 总结笔记
Initialization #include<iostream> #include<vector> using namespace std; int main() { vec ...