题目:

  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. Python哈希函数hashlib

    hashlib常用加密方法:md5(), sha1(), sha224(), sha356(), sha384(), sha512()等 结果显示方法: digest():    返回二进制字符串 h ...

  2. asp.net和脚本获取当前的URL、IP地址

    介绍一下ASP.NET取得当前页面的完整URL 的方放,icech做成了函数,直接用吧! private string GetPath() { string strPath = "http: ...

  3. showdialog窗体不在任务栏显示的问题处理

    场景: c#开发的windows窗体用showdialog弹出时,在任务栏中 win7系统显示,win xp和win 2003却不显示. 窗体的ShowInTaskbar已设置为True. 解决: 在 ...

  4. Linux LVM简明教程

    逻辑卷管理LVM是一个多才多艺的硬盘系统工具.无论在Linux或者其他类似的系统,都是非常的好用.传统分区使用固定大小分区,重新调整大小十分麻烦.但是,LVM可以创建和管理“逻辑”卷,而不是直接使用物 ...

  5. Redis几个认识误区(转)

    此文的作者是新浪微博平台架构师杨卫华(timyang)大师,如果关注了新浪一些牛人微博的同学应该知道,timyang前段时间正在对Redis进行一些研究和测试,也分享出了不少成果.下面一篇文章相信是t ...

  6. DBA_Oracle AWR Report性能监控报表(案例)

    2014-08-22 Created By BaoXinjian

  7. Centos 7 修改SSH端口号

    注意!这里的Centos版本是7   step1 修改/etc/ssh/sshd_config vi /etc/ssh/sshd_config #Port 22         //这行去掉#号 Po ...

  8. RVMDK的DEBUG调试-实时数据查看

    无论在Simulation还是硬件仿真的情况下,View-period windows update后watch窗口添加的变量即可实时更新, 软仿真和硬件仿真的区别就是实际时间的差异:如RTC查看秒的 ...

  9. Build常见错误

    1.ant 解决com.sun.tools.javac.Main is not on the classpath的问题 在ant打包时报错: 解决com.sun.tools.javac.Main is ...

  10. df和du显示的磁盘空间使用情况不一致的原因及处理

    在Linux下查看磁盘空间使用情况,最常使用的就是du和df了.然而两者还是有很大区别的,有时候其输出结果甚至非常悬殊. 1. 如何记忆这两个命令 du-Disk Usage df-Disk Free ...