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. ios cocos2d TexturePacker生成文件后的使用方法

    (1)将*.pvr.ccz文件转换为CCSpriteBatchNode (2)   将对应的plist文件读到CCSpriteFrameCache中 (3) 从CCSpriteFrameCache获取 ...

  2. Codeforces Round #191 (Div. 2) E题

    状态压缩DP,算sum,本来是枚举的,结果TLE了.. #include <iostream> #include <cstring> #include <cstdio&g ...

  3. BZOJ3236: [Ahoi2013]作业

    Description Input Output Sample Input 3 4 1 2 2 1 2 1 3 1 2 1 1 1 3 1 3 2 3 2 3 Sample Output 2 2 1 ...

  4. loadView、viewDidLoad及viewDidUnload的关系

      标题中所说的3个方法,都是UIViewController的方法,跟UIViewController的view属性的生命周期息息相关.接下来我会一一阐述它们的作用以及它们之间的联系. loadVi ...

  5. shell中的函数、数组

    函数定义: 数组:

  6. 滴答数必须介于 DateTime.MinValue.Ticks 和 DateTime.MaxValue.Ticks 之

    一个莫名其妙的问题:错误 滴答数必须介于 DateTime.MinValue.Ticks 和 DateTime.MaxValue.Ticks 之间. 参数名:ticks.这 网上找了很多,都没有一个正 ...

  7. Error 2147943712 during task creation

    In a Windows 2008 server, when you confirm the creation of a new task, you may obtain the following ...

  8. CSV to XLSX (专用)

    $csvFile = "F:\ACL\HZ ACL\ACL-APAC.CSV" $path = "F:\ACL\HZ ACL\ACL-APAC.XLSX" $r ...

  9. 【新产品发布】【iCore2 ARM / FPGA 双核心板】

    iCore2是一款包含ARM / FPGA两大利器的双核心板.ARM方面,采用意法半导体高性能的32位Cortex-M3内核STM32F103VE微处理器,主频达72MHz,并包含丰富外设接口.FPG ...

  10. var object dynamic的区别

    一.var var本身不是一种类型,只是一种语法糖:var声明的变量在赋值的时候即已决定其变量类型,编译时会进行校验. 二.object object是所以类型的基类,故可以赋任何类型的值. 三.dy ...