【LeetCode】8. 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.
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 Solution {
public int myAtoi(String str) {
str=str.trim();
char[] arr=str.toCharArray();
if(arr.length==0){
return 0;
} int i=0;
boolean symbol=true;
if(arr[i]=='-'){
symbol=false;
i++;
}else if(arr[i]=='+'){
i++;
} long MAX_VALUE=Integer.MAX_VALUE;
long MIN_VALUE=Integer.MIN_VALUE;
long num=0;
for(int j=i;j<arr.length;j++){
if(arr[j]>='0'&&arr[j]<='9'){
num=num*10+arr[j]-'0';
}else{
break;
} if(!symbol&&(0-num<MIN_VALUE)){
return Integer.MIN_VALUE;
}else if(symbol&&(num>MAX_VALUE)){
return Integer.MAX_VALUE;
}
} return symbol?new Long(num).intValue():new Long(0-num).intValue();
}
}
【LeetCode】8. String to Integer (atoi) 字符串转整数的更多相关文章
- [LeetCode] 8. 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)字符串转整数
Implement atoi which converts a string to an integer. The function first discards as many whitespace ...
- 【LeetCode】String to Integer (atoi)(字符串转换整数 (atoi))
这道题是LeetCode里的第8道题. 题目要求: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们 ...
- [LeetCode] String to Integer (atoi) 字符串转为整数
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- 【LeetCode】8. String to Integer (atoi) 字符串转换整数
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字符串转整数,atoi,题解,Leetcode, 力扣,P ...
- Leetcode8.String to Integer (atoi)字符串转整数(atoi)
实现 atoi,将字符串转为整数. 该函数首先根据需要丢弃任意多的空格字符,直到找到第一个非空格字符为止.如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连续的数字组合起来,这部分字 ...
- Leetcode 8 String to Integer (atoi) 字符串处理
题意:将字符串转化成数字. 前置有空格,同时有正负号,数字有可能会溢出,这里用long long解决(leetcode用的是g++编译器),这题还是很有难度的. class Solution { pu ...
- LeetCode OJ String to Integer (atoi) 字符串转数字
#include <iostream> #include <assert.h> using namespace std; int ato(const char *str) { ...
- 008 String to Integer (atoi) 字符串转换为整数
详见:https://leetcode.com/problems/string-to-integer-atoi/description/ 实现语言:Java class Solution { publ ...
随机推荐
- (转) 在Eclipse中进行C/C++开发的配置方法(20140721最新版)
本文转载自:http://blog.csdn.net/baimafujinji/article/details/38026421 Eclipse 是一个开放源代码的.基于Java的可扩展开发平台.就其 ...
- svn 分支
网上的SVN分支的教程真的不好用,我这里自己写的,绝对靠谱: SVN的分支跟GIT的分支不一样,SVN的分支,包括文件夹的分支或者是文件的分支,都是重复复制文件的,步骤如下: 1.branch/tag ...
- Spring整合activiti-modeler5.16遇到的小问题
接上一篇整合activiti-modeler并成功创建model:Spring整合activiti-modeler5.16 之后,我尝试运用自定义的model部署流程,但是在部署的过程中又遇到了一 ...
- SQL server 学习笔记1
1.查询安装的排序规则选项喝当前的排序规则服务器属性 select * from fn_helpcollations(); 2.查看当前服务器的排序规则 select serverproperty(' ...
- Android SDK的docs访问速度很慢(新)
#设置环境变量 名称:ANDROID_SDK_HOME 值:我的为-->E:\android\android-sdk #代码编译及运行 1.把下面的代码保存为:AndroidDocRepair. ...
- java.util.concurrent
软件包 java.util.concurrent 的描述 在并发编程中很常用的实用工具类.此包包括了几个小的.已标准化的可扩展框架,以及一些提供有用功能的类,没有这些类,这些功能会很难实现或实现起来冗 ...
- UCOS-消息邮箱(学习笔记)
任务间数据传递通过缓冲区进行,如果将缓冲区赋值给时间控制块成员:指针OSEventPtr,且任务控制块类型为OS_EVENT_TYPE_MBOS则即为消息邮箱: 一创建消息邮箱:OS_EVENT *O ...
- Servlet3.0学习总结(二)——使用注解标注过滤器(Filter)
Servlet3.0提供@WebFilter注解将一个实现了javax.servlet.Filter接口的类定义为过滤器,这样我们在web应用中使用过滤器时,也不再需要在web.xml文件中配置过滤器 ...
- git fetch和git pull(转载)
From:http://www.tech126.com/git-fetch-pull/ Git中从远程的分支获取最新的版本到本地有这样2个命令: 1. git fetch:相当于是从远程获取最新版本到 ...
- Yii 框架表单验证---实例