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.

问题:将字符窜转换成数字
分析:感觉题目不难,但是细节很多,容易想不到
1.数字前面有空格 如s=“    123456”
2.数字前出现了不必要或多于1个的字符导致数字认证错误,输出0   如s=“   b1234”  ,s=“  ++1233” , s=“ +-1121”
3.数字中出现了不必要的字符,返回字符前的数字 如s=“   12a12” , s=“ 123  123”
4.数字越界 超过了范围(-2147483648--2147483647) 若超过了负数的 输出-2147483648  超过了正数的输出2147483647
在科普一个知识点,倘若某个数超过了2147483647则会变为负数,反过来一样

 class Solution {
public:
int myAtoi(string str) {
long long result = ;
int i = ;
int flag1 = ;
int flag2 = ;
if(str.empty()) return ;
while(str[i] != '\0' && str[i] == ' ') i++;
while(str[i] == '-') { flag1++;i++; }
while(str[i] == '+') { flag2++;i++; }
while(i < str.length())
{
if(str[i] >= '' && str[i] <= '')
{
if(flag1 > ||flag2> || (flag1 == &&flag2 == ))
{
result = ;
break;
}
else if(flag1 == )
{
result = result * - (str[i]-'');
if(result < INT_MIN) result = INT_MIN;
}
else
{
result = result * + (str[i]-'');
if(result > INT_MAX) result = INT_MAX;
}
i++;
}
else
{
break;
}
}
int num = (int)result;
return num;
}
};

(letcode)String to Integer (atoi)的更多相关文章

  1. 【leetcode】String to Integer (atoi)

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

  2. No.008 String to Integer (atoi)

    8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...

  3. leetcode第八题 String to Integer (atoi) (java)

    String to Integer (atoi) time=272ms   accepted 需考虑各种可能出现的情况 public class Solution { public int atoi( ...

  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. String to Integer (atoi) - 字符串转为整形,atoi 函数(Java )

    String to Integer (atoi) Implement atoi to convert a string to an integer. [函数说明]atoi() 函数会扫描 str 字符 ...

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

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

  7. LeetCode--No.008 String to Integer (atoi)

    8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...

  8. leetcode-algorithms-8 String to Integer (atoi)

    leetcode-algorithms-8 String to Integer (atoi) Implement atoi which converts a string to an integer. ...

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

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

随机推荐

  1. BZOJ3251 : 树上三角形

    BZOJ AC1000题纪念~~~ 将x到y路径上的点权从小到大排序 如果不存在b[i]使得b[i]+b[i+1]>b[i+2]则无解 此时b数列增长速度快于斐波那契数列,当达到50项时就会超过 ...

  2. 使用HttpsURLConnection发送POST请求

    重写X509TrustManager private static TrustManager myX509TrustManager = new X509TrustManager() { @Overri ...

  3. 手机开发必备技巧:javascript及CSS功能代码分享

    1. viewport: 也就是可视区域.对于桌面浏览器,我们都很清楚viewport是什么,就是出去了所有工具栏.状态栏.滚动条等等之后用于看网页的区域,这是真正有效的区域.由于移动设备屏幕宽度不同 ...

  4. maven工程代码关联源代码配置

    最近在学习dubbo,在maven构建完成后,需要关联查看一些依赖jar的源码,配置很简单,如下: 勾选windows-Preferences-Maven- Download Artifact Sou ...

  5. Toll-Free Bridge

    引 在深入了解桥接机制的时候看到一篇好文,虽然已经很久远,但是忍不住看了好几遍,心中诸多不解一扫而光.在此放上链接: 原文:http://ridiculousfish.com/blog/posts/b ...

  6. 微课程--Android--Activity组建与Intent

    安卓的四大基本组件 Activity是经常使用的组件 1 展示用户界面 2 响应用户操作 Service 1 在后台长时间运行 2 没有用户界面 ContentProvider 1 管理和共享应用数据 ...

  7. shell 循环

    for循环: 批量删除.gz结尾的文件: 循环打包文件并备份到一个目录下面: find ./ -maxdepth 1 -name "*.gz" find ./ -maxdepth ...

  8. shiyan2

    <?xml version="1.0" encoding="ISO-8859-1"?><web-app xmlns="http:// ...

  9. 找1到n所有整数出现1的个数

    编程之美2.4 n=12时,1,11,12这3个数包含1,所以1的个数是5. Line 9是为了防止factor溢出. #include <iostream> #include <s ...

  10. MongoDB数据备份与恢复

    测试环境:windows 一. 导出数据F:\DbSoft\soft\master\bin>mongoexport /h 127.0.0.1 /port 50000 /d testdb /c t ...