leetcode — string-to-integer-atoi
/**
* Source : https://oj.leetcode.com/problems/string-to-integer-atoi/
*
* Created by lverpeng on 2017/7/4.
*
* 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.
*
*
* 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 class IntegerParser {
/**
* 将字符串转化为int
* 考虑各种输入:
* 输入包含非数字
* 超出int范围
* 可能是负数
*
* @param str
* @return
*/
public int parse (String str) throws Exception {
if (str == null || str.length() == 0) {
return 0;
}
int result = 0;
boolean flag = true;
if (str.charAt(0) == '-') {
flag = false;
str = str.substring(1);
} else if (str.charAt(0) == '+') {
flag = true;
str = str.substring(1);
}
for (int i = 0; i < str.length(); i++) {
if (!isdDigit(str.charAt(i))) {
throw new IllegalArgumentException("is not a number");
}
if (result > Integer.MAX_VALUE / 10 || result < Integer.MIN_VALUE / 10) {
throw new Exception("overflow");
}
result = result * 10 + str.charAt(i) - 48;
}
if (!flag) {
result = 0 - result;
}
return result;
}
private boolean isdDigit (char c) {
if (c >= 48 && c <= 57) {
return true;
}
return false;
}
public static void main(String[] args) throws Exception {
IntegerParser parser = new IntegerParser();
System.out.println(parser.parse("123"));
System.out.println(parser.parse("+123"));
System.out.println(parser.parse("-123"));
System.out.println(parser.parse("123ABC"));
}
}
leetcode — string-to-integer-atoi的更多相关文章
- LeetCode: String to Integer (atoi) 解题报告
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- [LeetCode] String to Integer (atoi) 字符串转为整数
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- [Leetcode] String to integer atoi 字符串转换成整数
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- [LeetCode] String to Integer (atoi) 字符串
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- [Leetcode]String to Integer (atoi) 简易实现方法
刚看到题就想用数组做,发现大多数解也是用数组做的,突然看到一个清新脱俗的解法: int atoi(const char *str) { ; int n; string s(str); istrings ...
- [LeetCode]String to Integer (atoi)
题意:字符串转正数 原题来自:https://leetcode.com/problems/string-to-integer-atoi/ 分析: <程序员面试宝典>上出现的面试题,主要是考 ...
- leetcode String to Integer (atoi) python
class Solution(object): def myAtoi(self, str): """ :type str: str :rtype: int "& ...
- 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 ...
- Kotlin实现LeetCode算法题之String to Integer (atoi)
题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 class ...
- Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)
Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...
随机推荐
- journalctl 清理journal日志
在CentOS 7开始使用的systemd使用了journal日志,这个日志的管理方式和以往使用syslog的方式不同,可以通过管理工具维护. 使用df -h检查磁盘文件,可以看到/run目录下有日志 ...
- linux就该这么学,第十一天了
今天讲了,网卡绑,定,两块网卡同时工作,自动备源,理论上速度提升一倍,工作中可以用到的技术 还有sshd服务,端口22,远程连接使用,还可以设置root是否可以直接登录,主要配置文件在,/etc/ss ...
- 命令方式联网与界面network-manager方式联网
命令方式联网: sudo vi /etc/NetworkManager/NetworkManager.conf [main]plugins=ifupdown,keyfile,ofonodns=dnsm ...
- 部署文档(centos7.x\nginx\mysql5.6\jdk1.8\ssl\jboot)
部署文档(centos7.x\nginx\mysql5.6\jdk1.8\ssl\jboot) 1.基础环境********************************************** ...
- boost--日期处理
1.timer 不同于系统函数的timer()一般生成一个定时器,boost中的timer是一个计时器,以秒为单位,最小精度为毫秒,使用需要包含头文件"boost\timer.hpp&quo ...
- JavaScript基础视频教程总结(021-030章)
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- Ubuntu 14.04 LTS 初装成
原先博客放弃使用,几篇文章搬运过来 Windows 7下使用win32diskimager 制作启动盘,安装Ubuntu OS安装完成后,安装DrclientLinux. 安装搜狗输入法 Linux下 ...
- pyinstaller打包程序 带图片
首选说一下,这种打包方式只能在本电脑上使用运行正常 准备:.py文件:你的程序 gif文件:你要用的图片 第一步: 在上面文件所在目录打开cmd 输入 pyi-makespec FP.py 会生成一个 ...
- 推荐使用OpenLiveWriter在cnblogs上写的Blog
这是我第一个使用OpenLiveWriter在cnblogs上写的Blog.不知道效果如何,但又很多功能我可以采用! 如表格功能: Open Live Writer Write on Web 优 ...
- 在Git中设置自己的姓名
在Git中,自己的姓名与每一个commit提交绑定在一起.如果你在使用Azure DevOps Server中的Git Repo时,一定要注意commit中的提交者与服务器上的推送者,是两个概念. 在 ...