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 ...
随机推荐
- 数字提取——C语言
Problem Description AekdyCoin is the most powerful boy in the group ACM_DIY, whose signature is valu ...
- IP、TCP、DNS协议
·······················································IP协议························· 位于网络层,作用是把数据包传送 ...
- 对hadoop namenode -format执行过程的探究
引言 本文出于一个疑问:hadoop namenode -format到底在我的linux系统里面做了些什么? 步骤 第1个文件bin/hadoop Hadoop脚本位于hadoop根目录下的bi ...
- php5.6 phpmystudy 版本出问题
No input file specified的解决方法 https://jingyan.baidu.com/article/f7ff0bfccce11c2e26bb1381.html
- C# Form Chart X刻度左右多余一格怎么去掉
如上图所示:形成的chart,1和8时y没有值,我实际给的也是2~7的数,可视1和8的刻度却在,怎么去掉,谢谢. 解决方法:chart1.ChartAreas[0].AxisX.IsMarginVis ...
- Java多线程系列1 线程创建以及状态切换
我们知道线程线程有三种创建方式 1实现Runnable接口 2 继承Thread类 3使用Callable和Future接口创建线程.具体是创建Callable接口的实现类,并实现clall()方法. ...
- 64位版本为什么叫amd64,而不是intel64
64位版本为什么叫amd64,而不是intel64? 首先了解下常见的几个架构: X86是一个指令集,是刚有个人电脑时候的什么8086,286,386的那个兼容的指令集. “x86-64”,有时会 ...
- EF6 学习笔记(二):操练 CRUD 增删改查
EF6学习笔记总目录 ASP.NET MVC5 及 EF6 学习笔记 - (目录整理) 接上篇: EF6 学习笔记(一):Code First 方式生成数据库及初始化数据库实际操作 本篇原文链接: I ...
- 添加自己的component
在之前讲解esp-idf的文件结构时,曾经讲过component是esp-idf集成的功能块,这篇文章就来讲解下,如何在esp-idf 中添加自己的component. STEP1; 创建compon ...
- Thinking in Java from Chapter 11
From Thinking in Java 4th Edition 持有对象 // Simple container example (produces compiler warnings.) // ...