题目:

  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.

思路:

  该题目是说将string类型的字符串转换成整型数据,类似于C++库里的atoi函数,解决该题目的关键在于两个方面:
  (1)字符串格式的合法判断
  (2)转换结果的溢出判断
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) 字符串转整数的更多相关文章

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

    Implement atoi which converts a string to an integer. The function first discards as many whitespace ...

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

    Implement atoi which converts a string to an integer. The function first discards as many whitespace ...

  3. 【LeetCode】String to Integer (atoi)(字符串转换整数 (atoi))

    这道题是LeetCode里的第8道题. 题目要求: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们 ...

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

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

  5. 【LeetCode】8. String to Integer (atoi) 字符串转换整数

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字符串转整数,atoi,题解,Leetcode, 力扣,P ...

  6. Leetcode8.String to Integer (atoi)字符串转整数(atoi)

    实现 atoi,将字符串转为整数. 该函数首先根据需要丢弃任意多的空格字符,直到找到第一个非空格字符为止.如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连续的数字组合起来,这部分字 ...

  7. Leetcode 8 String to Integer (atoi) 字符串处理

    题意:将字符串转化成数字. 前置有空格,同时有正负号,数字有可能会溢出,这里用long long解决(leetcode用的是g++编译器),这题还是很有难度的. class Solution { pu ...

  8. LeetCode OJ String to Integer (atoi) 字符串转数字

    #include <iostream> #include <assert.h> using namespace std; int ato(const char *str) { ...

  9. 008 String to Integer (atoi) 字符串转换为整数

    详见:https://leetcode.com/problems/string-to-integer-atoi/description/ 实现语言:Java class Solution { publ ...

随机推荐

  1. Sublime text2用户自定义配置

    [{ "keys": ["ctrl+d"], "command": "run_macro_file", "ar ...

  2. 华人曾与IBM抗衡! 盘点已远去的IT巨头(转)

    [PConline资讯 ]从算盘到计算器,从大型机到个人PC,再到当今火热的移动终端和云计算,人类计算史已经走过了千年之久,现代IT计算领域也经过了百年浮沉.在世界工业领域,IT技术应该是诞生时间最短 ...

  3. 从网站上复制代码到MyEclipse后每行都是字符编码错误的解决办法

    注意图中的工具栏的按钮:Show Whitespace Characters(Window->Preferences->General->Editors->Text Edito ...

  4. JavaScript正则详谈

    JavaScript RegExp 基础详谈   前言: 正则对于一个码农来说是最基础的了,而且在博客园中,发表关于讲解正则表达式的技术文章,更是数不胜数,各有各的优点,但是就是这种很基础的东西,如果 ...

  5. C#托管代码与C++非托管代码互相调用

    http://www.cnblogs.com/Jianchidaodi/archive/2009/03/11/1407270.html#1473515 http://www.cnblogs.com/J ...

  6. PLSQL_Database Link的基本概念和用法(概念)

    2014-06-08 Created By BaoXinjian

  7. redis 数据持久化

    1.快照(snapshots) 缺省情况情况下,Redis把数据快照存放在磁盘上的二进制文件中,文件名为dump.rdb.你可以配置Redis的持久化策略,例如数据集中每N秒钟有超过M次更新,就将数据 ...

  8. apache/php 开启 gzip压缩

    1.php方式开启 原理: header("Content-Encoding: gzip"); echo gzencode('songjiankang'); 示例1: functi ...

  9. shell中实现自动登录(bash环境脚本中)

    自己的脚本: #!/bin/bash expect  -c  "     set timeout 3600;     spawn su -;     expect *assword:*;  ...

  10. 移动设备wap手机网页html5通过特殊链接:打电话,发短信,发邮件详细教程

    如果需要在移动浏览器中实现拨打电话,调用sms发短信,发送email等功能,移动手机WEB页面(HTML5)Javascript提供的接口是一个好办法. 采用url href链接的方式,实现在Safa ...