8.String to Integer (atoi)
2015-08-15 15:11
330 查看
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.
Update (2015-02-10):
The signature of the
function signature accepts a
button to reset your code definition.
spoilers alert... click to show requirements for atoi.
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.
Solution: be careful for all the situation.
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.
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.
Solution: be careful for all the situation.
public class Solution { public int myAtoi(String str) { if (str == null) { return 0; } str = str.trim(); if (str.length() == 0) { return 0; } int flag = 0; double re = 0; if (str.charAt(0) == '-') { flag = 1; } else if (str.charAt(0) == '+') { } else if ((str.charAt(0) >= '0' && str.charAt(0) <= '9')) { re = 10*re + (str.charAt(0)-'0'); } else { return 0; } for (int i = 1;i < str.length();i++) { if (str.charAt(i) >= '0' && str.charAt(i) <= '9') { re = 10*re + (str.charAt(i)-'0'); } else { break; } } if (flag == 1) { re = -re; } if (re > Integer.MAX_VALUE) { return Integer.MAX_VALUE; } else if (re < Integer.MIN_VALUE) { return Integer.MIN_VALUE; } else { return (int)re; } } }
相关文章推荐
- 分布式与集群的联系与区别
- 修改linux系统默认启动级别
- hdu 1588
- oracle创建user具体指示
- 一套完整系统对人生的意义
- 黑马程序员----包(package)
- spring常用的一些注解以及注解注入总结
- codeforces 569 E. New Language (2-sat)
- VMware Workstation11.0安装Mac OS X 10.10最完整指南(包含所需所有资源下载)
- 常用变量类型
- 裁减图片
- 苹果开发 笔记(61)recursiveDescription 方法
- Android透明状态栏(沉浸式状态栏)
- NGUI文本之中穿插图片
- ! [remote rejected] HEAD -> refs/for/ToonV3.0 (missing Change-Id in commit message footer)
- DSP数据运算基础
- 锁与分区
- find 命令结合 正则表达式
- poj 3522 Slim Span(最小生成树)kruskal算法
- SAP实施的难点在哪里?