【LeetCode-面试算法经典-Java实现】【008-String to Integer (atoi) (字符串转成整数)】
【008-String to Integer (atoi) (字符串转成整数)】
【LeetCode-面试算法经典-Java实现】【全部题目文件夹索引】
原题
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.
题目大意
实现一个atoi函数,将字符串转成整形
要点:考虑全部的输入情况。
解题思路
前导字符是+或-或者没有。接下来输入的是数字,数字不能整数能表示的最大或最小数。假设超过就返回相应的最小或者最小的值。
代码实现
public class Solution {
public int atoi(String str) {
if (str == null || str.length() == 0) {
// throw new NumberFormatException("Invalid input string: " + str);
return 0;
}
// 假设字符串以空格開始
int start = 0; //从開始找第一个不是空格的数
boolean positive = true; // 是否为正数默觉得true
if (str.charAt(start) == ' ') {
while (str.charAt(start) == ' ') {
start++;
if (start >= str.length()) { // 输入的全是空格
// throw new NumberFormatException("Invalid input string: " + str);
return 0;
}
}
}
if (str.charAt(start) == '-') { // 第一个非空白字符中-
positive = false;
start++;
} else if (str.charAt(start) == '+') {// 第一个非空白字符是+
start++;
} else if (str.charAt(start) >= '0' && str.charAt(start) <= '9') { // 第一个非空白字符是数字
return cal(str, start, true);
} else { // 其他情况就抛出异常
// throw new NumberFormatException("Invalid input string: " + str);
return 0;
}
if (start >= str.length()) { // 第一个非空白字符是+或者-但也是最后一个字符
// throw new NumberFormatException("Invalid input string: " + str);
return 0;
}
if (str.charAt(start) > '9' || str.charAt(start) < '0') { // +或者-后面接的不是数字
// throw new NumberFormatException("Invalid input string: " + str);
return 0;
} else {
return cal(str, start, positive);
}
}
private int cal(String str, int start, boolean positive) {
long result = 0;
while (start < str.length() && str.charAt(start) >= '0' && str.charAt(start) <= '9') {
result = result * 10 + (str.charAt(start) - '0');
if (positive) { // 假设是正数
if (result > Integer.MAX_VALUE) {
// throw new NumberFormatException("Invalid input string: " + str);
return Integer.MAX_VALUE;
}
} else {
if (-result < Integer.MIN_VALUE) {
// throw new NumberFormatException("Invalid input string: " + str);
return Integer.MIN_VALUE;
}
}
start++;
}
if (positive) {
return (int) result;
} else {
return (int) -result;
}
}
}
评測结果
点击图片,鼠标不释放。拖动一段位置,释放后在新的窗体中查看完整图片。
特别说明
欢迎转载,转载请注明出处【http://blog.csdn.net/derrantcm/article/details/46938417】
【LeetCode-面试算法经典-Java实现】【008-String to Integer (atoi) (字符串转成整数)】的更多相关文章
- [Leetcode] String to integer atoi 字符串转换成整数
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- 8. String to Integer (atoi) 字符串转成整数
[抄题]: Input: "42" Output: 42 Example 2: Input: " -42" Output: -42 Explanation: T ...
- 008 String to Integer (atoi) 字符串转换为整数
详见:https://leetcode.com/problems/string-to-integer-atoi/description/ 实现语言:Java class Solution { publ ...
- 【LeetCode每天一题】String to Integer (atoi)(字符串转换成数字)
Implement atoi which converts a string to an integer.The function first discards as many whitespace ...
- 【LeetCode】8. String to Integer (atoi) 字符串转换整数
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字符串转整数,atoi,题解,Leetcode, 力扣,P ...
- 【LeetCode】String to Integer (atoi)(字符串转换整数 (atoi))
这道题是LeetCode里的第8道题. 题目要求: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们 ...
- No.008 String to Integer (atoi)
8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...
- String to Integer (atoi) - 字符串转为整形,atoi 函数(Java )
String to Integer (atoi) Implement atoi to convert a string to an integer. [函数说明]atoi() 函数会扫描 str 字符 ...
- LeetCode--No.008 String to Integer (atoi)
8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...
- 【Java】 剑指offer(67) 把字符串转换成整数
本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集 题目 请你写一个函数StrToInt,实现把字符串转换成整数这个功能 ...
随机推荐
- Introduction to MongoDB
https://docs.mongodb.com/getting-started/csharp/introduction/ MongoDB is an open-source document dat ...
- 可以通过shadowserver来查看开放的mdns(用以反射放大攻击)——中国的在 https://mdns.shadowserver.org/workstation/index.html
Open mDNS Scanning Project 来自:https://mdns.shadowserver.org/ If you are looking at this page, then m ...
- zzulioj--1822--水水更健康(水题)
1822: 水水更健康 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 49 Solved: 19 SubmitStatusWeb Board Des ...
- visual studio code(vscode)的使用(快捷键)
Visual Studio Code初探 vscode 是一种可运行于 OS X,Windows 和 Linux 之上的免费跨平台编辑器: 1. 快捷键 ctrl + `:调出(对于 windows ...
- iOS开发—— UIMenuController的使用
UIMenuController的展现需要基于一个View视图,其交互则需要基于其所在View视图的Responder.举例来说,如果一个UIMenuController展现在当前ViewContr ...
- PHP DES-ECB加密对接Java解密
最近公司有个业务,需要对接第三方接口,但是参数是需要加密的,对方也只提供了一个java的demo,在网上到处搜索,没有找到直接就能用的方法,后来还是跟公司的Android工程师对接出来的,在这里记录一 ...
- linux学习之多高并发服务器篇(一)
高并发服务器 高并发服务器 并发服务器开发 1.多进程并发服务器 使用多进程并发服务器时要考虑以下几点: 父最大文件描述个数(父进程中需要close关闭accept返回的新文件描述符) 系统内创建进程 ...
- 题解 BZOJ4919 【大根堆】
题面:传送门. 老师说今天要考一道线段树合并,然后...然后这道题我就GG了.(当然可以用线段树合并写,只是比较复杂) 有人赛时想了个贪心,然后被机房巨佬hack了,结果在hack的过程中巨佬想出了正 ...
- 题解 CF383C 【Propagating tree】
这道题明明没有省选难度啊,为什么就成紫题了QAQ 另:在CF上A了但是洛谷Remote Judge玄学爆零. 思路是DFS序+线段树. 首先这道题直观上可以对于每一次修改用DFS暴力O(n),然后对于 ...
- 【Henu ACM Round#20 C】 Eevee
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 处理处所有的字符串可能的样子. 存在map里面就好. [代码] #include <bits/stdc++.h> usi ...