Implement atoi to convert a string to an integer.

题目分析:

题目本身很简单就是将一个字符串转化成一个整数,但是由于字符串的千差万别,导致在实现的时候细节非常多。需要注意的有以下一些:
1.字符串可能由一些空格开始,然后遇到一个正号或者负号,然后是正常的数字
” +123” -> 123 ;” -41” -> -41
2.如果在数字后出现了一些其他字符,直接忽视
” +123^&34” -> 123 ; ” -41 123” -> -41
3.如果最前面的字符是无效数字,或者字符串只有空格或者是空的,都直接返回0
” &*^20” -> 0
4.如果正确的数字超出了int,如果是大于INT_MAX (2147483647),就返回2147483647;如果小于INT_MIN (-2147483648) ,就返回INT_MIN (-2147483648)
“2147483648” -> 2147483647

class Solution {
public:
int myAtoi(string str) {
long long sum=0;
int flag=0;//判断是否是第一个非空格字符
int fuhao=0;//如果等于1,表明是负数
int mine=-2147483648;//最小int
int maxe=2147483647;//最大int for(int i=0;i<str.size();i++)
{
//如果目前为止还没有出现第一个非空格字符
if(str[i]==32 && flag==0) continue; if((str[i]==43) || (str[i]==45)) {//43:+ 45:-
if(flag==0)//这里flag==0为真时表示遇到第一个非空格字符
{
flag=1;
if(str[i]==45)fuhao=1;
continue;
}
else return 0;//非第一个非空格字符位,又出现正负号,就是无效数字
} //如果数字后面又出先了非数字字符,就自动忽略后面的字符,返回已统计的数字
if(str[i]<48 || str[i]>57) {
if(fuhao==1) return -sum;
else return sum;
} sum=sum*10+str[i]-48;//如果该位字符是正常的数字,就将这个数字加入统计
flag=1;//即使第一个非空格字符不是符号,也要明确已经出现了非空格字符 if(fuhao==0){
if(sum>maxe) return maxe;
}
else{
if(-sum<mine) return mine;
} }
if(fuhao==1) return -sum;
return sum; }
};

8. String to Integer (atoi) ---Leetcode的更多相关文章

  1. String to Integer (atoi) leetcode

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

  2. String to Integer (atoi) ---- LeetCode 008

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

  3. String to Integer (atoi) leetcode java

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

  4. leetcode day6 -- String to Integer (atoi) &amp;&amp; Best Time to Buy and Sell Stock I II III

    1.  String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully con ...

  5. Kotlin实现LeetCode算法题之String to Integer (atoi)

    题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 class ...

  6. LeetCode: String to Integer (atoi) 解题报告

    String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...

  7. Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)

    Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...

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

    8. 字符串转换整数 (atoi) 8. String to Integer (atoi) 题目描述 LeetCode LeetCode8. String to Integer (atoi)中等 Ja ...

  9. 【leetcode】String to Integer (atoi)

    String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...

随机推荐

  1. English-培训3-Please call me Beth

  2. Installation Manager1.8安装

    1.下载地址: https://www-01.ibm.com/marketing/iwm/iwm/web/download.do?S_PKG=500005026&source=swerpws- ...

  3. Oracle数据的导入与导出

    本文针对window操作系统与oracle12C的版本. 1.sqlplus执行单个sql文件 1.执行sqlplus登陆命令:sqlplus username/password@host:port/ ...

  4. Android笔记(十五) Android中的基本组件——单选框和复选框

    单选框和多选框通常用来在设置用户个人资料时候,选择性别.爱好等,不需要用户直接输入,直接在备选选项中选择,简单方便. 直接看代码: <?xml version="1.0" e ...

  5. Django之过滤器

    Django 过滤器   过滤器 描述 示例 upper 以大写方式输出 {{ user.name | upper }} add 给value加上一个数值 {{ user.age | add:”5” ...

  6. CentOS 7的Linux系统优化加固

    1.关闭selinux 2.关闭防火墙 3.关闭NetworkManager 4.为系统运维管理员创建普通用户,并配置sudo(vi  sudo) 5.清空泄漏系统版本信息的文件 6.基础优化sshd ...

  7. javascript reduce 前端交互 总计

    sum(){ return this.products.reduce((total,next)=>{ return total + next.price * next.aumout},0) } ...

  8. Nginx http升级到https

    http和https的区别是 有的网站,http打开的时候,页面提示不安全,比如你点击下面的网站 [其实是同一个网站] http://www.511easy.com/bug/login http:// ...

  9. PAT基础级-钻石段位样卷2-7-4 6翻了 (15 分)

    “666”是一种网络用语,大概是表示某人很厉害.我们很佩服的意思.最近又衍生出另一个数字“9”,意思是“6翻了”,实在太厉害的意思.如果你以为这就是厉害的最高境界,那就错啦 —— 目前的最高境界是数字 ...

  10. Mybatis3.1-[tp_34-35]-_映射文件_select_resultMap关联查询_collection定义关联集合封装规则_collection分步查询_延迟加载

    笔记要点出错分析与总结工程组织 1.定义接口 interface DepartmentMapper package com.dao; import com.bean.Department; publi ...