/**
* 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的更多相关文章

  1. LeetCode: String to Integer (atoi) 解题报告

    String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...

  2. [LeetCode] String to Integer (atoi) 字符串转为整数

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  3. [Leetcode] String to integer atoi 字符串转换成整数

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  4. [LeetCode] String to Integer (atoi) 字符串

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  5. [Leetcode]String to Integer (atoi) 简易实现方法

    刚看到题就想用数组做,发现大多数解也是用数组做的,突然看到一个清新脱俗的解法: int atoi(const char *str) { ; int n; string s(str); istrings ...

  6. [LeetCode]String to Integer (atoi)

    题意:字符串转正数 原题来自:https://leetcode.com/problems/string-to-integer-atoi/ 分析: <程序员面试宝典>上出现的面试题,主要是考 ...

  7. leetcode String to Integer (atoi) python

    class Solution(object): def myAtoi(self, str): """ :type str: str :rtype: int "& ...

  8. leetcode day6 -- String to Integer (atoi) &amp;&amp; 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 ...

  9. Kotlin实现LeetCode算法题之String to Integer (atoi)

    题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 class ...

  10. Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)

    Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...

随机推荐

  1. c++11 线程池学习笔记 (二) 线程池

    学习内容来自以下地址 http://www.cnblogs.com/qicosmos/p/4772486.html github https://github.com/qicosmos/cosmos ...

  2. MySQL—查询某时间范围的数据

    -- 查询今天的数据 select * from `user` where to_days(birthday) = to_days(CURDATE()); -- 查询昨天的数据 select * fr ...

  3. 2019.03.02 bzoj2565: 最长双回文串(pam)

    传送门 题意简述:问最长的由两个回文串连接而成最长字串长度. 思路: 正反串各建一个pampampam然后就完了. 代码: #include<bits/stdc++.h> #define ...

  4. php 将对象转化为数组

    $list = json_decode(json_encode($list), true);  

  5. windows下多tomcat部署

    两种方式: 第一种是修改tomcat下很多配置文件,且配置环境变量: 第二种不配tomcat环境变量,通过修改server.xml文件即可: 本篇讲解第二种方式: 首先说下如何修改tomcat控制台标 ...

  6. [Python] networkx入门 转

    networkx是python的一个第三方包,可以方便地调用各种图算法的计算. 通过调用python画图包matplotlib能实现图的可视化. 1.安装 正好整理一下python第三方包的安装方法. ...

  7. Apache Phoenix Flume集成 -- JsonEventSerializer改进

    背景 数据从kafka ingest到Phoenix.数据格式采取Json.数据链路: api -> kafka -> Flume -> Phoenix 官方JsonEventSer ...

  8. 音视频编解码——YUV视频格式详解

    一.YUV 介绍 YUV是一种颜色编码方方式,通常由彩色摄像机进行取像,然后把取得的彩色图像信号经过分色.分别放大校正后得到RGB,再经过矩阵变换得到亮度信号Y和两个色差信号B-Y(即U).R-Y(即 ...

  9. 腾讯云播放器更新——TCplayer

    概述 最近腾讯云播放器进行了更新,增加了TCplayer,支持点播播放.由于工作需要,了解了一下TCplayer,把心得记录下来,供以后开发时参考,相信对其他人也有用. 参考文档: TCPlayer开 ...

  10. Java匹马行天下之JavaSE核心技术——面向对象

    面向对象 注: 看此篇时强烈建议有一定的面向对象思想基础,有一定的基础后先翻到下面看第九条:      9.面向对象: 从未封装→封装→继承→多态→抽象类→接口的代码演变 按这个逻辑去看,,哪有不理解 ...