博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[leetcode]String to Integer (atoi)
阅读量:5807 次
发布时间:2019-06-18

本文共 2533 字,大约阅读时间需要 8 分钟。

问题描写叙述:

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.

思路:

实现字符串转换为int型整数并不难,仅仅是有好多中输入情况须要考虑,easy造成遗漏。

首先看一下c++ 中atoi的定义:

-----------------------------------

atoi

int atoi (const char * str);
Convert string to integer
Parses the C-string str interpreting its content as an integral number, which is returned as a value of type int.
The function first discards as many whitespace characters (as in ) 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 base-10 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 and zero is returned.

Return Value

On success, the function returns the converted integral number as an int value.
If the converted value would be out of the range of representable values by an int, it causes undefined behavior. See for a more robust cross-platform alternative when this is a possibility.

------------------------------------------

能够看出须要注意的有下面几点:

  1. 去掉前面的空格
  2. 注意開始时候的符号 + -
  3. 忽略数字后面的杂七杂八字符
  4. 考虑溢出的情况。

考虑了上面几方面。代码就能够AC了。

代码:

public class String_To_Integer { //java	public static int atoi(String str) {		int haveMinus = 1;		long result = 0;  				if(str == null || str.trim().isEmpty())			return 0;				//chech + - 		str = str.trim();		if(str.charAt(0) == '-'){			str = str.substring(1);			haveMinus = -1;		}else if(str.charAt(0) == '+')			str = str.substring(1);				//check num		for(int i = 0;i < str.length() && str.charAt(i) >= '0' && str.charAt(i) <= '9'; i++){			result = result*10 + (str.charAt(i)- '0');		}				//deal overflow		if(result > 2147483647 && haveMinus == 1)			return 2147483647;		if(result > 2147483647 && haveMinus == -1)			return -2147483648;				return haveMinus*(int)result;    }		public static void main(String [] args){		System.out.println(String_To_Integer.atoi("2147483648"));	}}

转载地址:http://wzubx.baihongyu.com/

你可能感兴趣的文章
python基础教程项目1:及时标记
查看>>
Java-boolean类型
查看>>
分发列表实现路由控制过滤(RIP)实验
查看>>
我的友情链接
查看>>
shell【分发系统】
查看>>
免费DNS服务地址
查看>>
Java调优—Btrace监控Java线程/方法执行参数、执行时间(Windows)
查看>>
MySQL操作
查看>>
GitHub专用工具
查看>>
js实现按钮复制功能
查看>>
1、下载安装scala编译器(可以理解为scala的jdk),地址:http://www.scala
查看>>
mui 总结2--新建第一个app项目
查看>>
nginx的lua api
查看>>
考研太苦逼没坚持下来!看苑老师视频有点上头
查看>>
【安全牛学习笔记】提权
查看>>
HCNA——RIP的路由汇总
查看>>
网络地址转换——NAT
查看>>
老男孩LIUNX36期学员-方秀升决心书
查看>>
详解JS对象
查看>>
Python--字符串
查看>>