[leetcode 8] String to Integer
1 题目:
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.
Update (2015-02-10):
The signature of the C++ function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button to reset your code definition.
spoilers alert... click to show requirements for atoi.
The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.
The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.
If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.
If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.
- 前面有空格
- 整型越界
- 有小数点
- 判断正负
- 非数字字符的处理,包括数字前面和后面
public int myAtoi(String str) {
if(str.equals("")) return 0;
//1 handle space
str = str.trim();
char[] chars = str.toCharArray();
int len = str.length();
long number = 0;
boolean sign = true;
// handle sign
if(chars[0] >= '0' && chars[0] <= '9'){
number += chars[0] - 48;
}else if(chars[0] == '-'){
sign = false;
}else if(chars[0] == '+'){
sign = true;
}else{
return 0;
}
// convern to number
for(int i = 1; i < len; i++){
if(chars[i] >= '0' && chars[i] <= '9'){
number = number*10 + (chars[i] - 48);
//handle overflow
if(number > Integer.MAX_VALUE){
return sign ? Integer.MAX_VALUE : Integer.MIN_VALUE;
}
}else{
number = sign ? number : -number;
return (int)number;
}
}
number = sign ? number : -number;
return (int)number;
}
[leetcode 8] String to Integer的更多相关文章
- 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 8 String to Integer (string转int)
题目来源:https://leetcode.com/problems/string-to-integer-atoi/ Implement atoi to convert a string to an ...
- [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 ...
随机推荐
- c#对dataset和list集合压缩和解压,能提高访问速度
public class YS { public static byte[] Decompress(byte[] data) { byte[] bData; MemoryStream ms = new ...
- DOM BOM 常用API小记
DOM 1.元素节点: 元素节点element: 更精确的获得元素的标签名(全大写) 属性节点attribute: 属性名 文本节点text: #text 注释节点document:#document ...
- 如何烧写BIOS到SD卡里面
针对TINY6410 ADK型号 1.SD卡格式化为FAT32或者FAT格式 2.将SD卡插入USB接口的读卡器,并插在PC的USB口 3.“以管理员身份运行”SD-Flasher.exe(在tiny ...
- java 中java.util.Arrays类---常用函数记录
java.util.Arrays主要是用来对数组进行操作的类,主要包括以下方法: 1.数组转化列表,得到固定大小的列表,Arrays.asList(...): public static <T& ...
- spring学习 十六 spring加载属性文件
第一步:创建一个properties文件,以数据库链接作为实例db.properties jdbc.url=jdbc:mysql://192.168.153.128:3306/mybaties?cha ...
- web前端面试题库
web前端面试题及答案 1.常用那几种浏览器测试?有哪些内核(Layout Engine)? 答: (Q1) 浏览器:IE,Chrome,FireFox,Safari,Opera. (Q2) ...
- Linux运维之docker虚拟化部署nginx
一.Docker的概念 Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化.容器是完全使用沙箱 ...
- 52.tableViewCell重用机制避免重复显示问题
表刷新超出页面显示的内容会重复出现 -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSInd ...
- 对象序列化:pickle和shelve
import pickle class DVD: def __init__(self,tilte,year=None,duration=None,director_id=None): self.tit ...
- PC Access的使用
需要copy xxx.dll 到windows/syswow64 目录下 运行com注册 启动电脑后,自动锁定(在启动目录下架锁定程序) using System; using System.Col ...