leetcode编程记录1 #8 String to Integer (atoi)
2017-09-09 15:56
507 查看
今天是第一次记录在leetcode上的做题的解题过程。
先看第一题:
String to Integer (atoi)
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.
解题分析:
首先要理解到题目要求我们实现的是一个能够将ASCII码中数字转换成数字的函数,这个函数就是在c库函数中常用的atoi函数,这个函数将字符串转换成整型数字的时候有四点要求需注意:
该函数自动忽略字符串开始的空格符和制表符,直至遇到正负号或数字则开始转换。
当遇到正负号或数字后转换开始,这之后如果遇到非数字的字符则结束转换。
如果字符串开始不是空格符,制表符,正负号或者数字则函数的返回值为0
如果字符串所表示的数字超出了int数据所能表示的范围则当数字上溢时返回INT_MAX,下溢出时则返回INT_MIN。
根据这四个要求可以开始对字符串开始处理了,下列是我的c++代码,因为一开始没有考虑到一些情况所以到后来修改完成的代码有一些臃肿。
先看第一题:
String to Integer (atoi)
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.
解题分析:
首先要理解到题目要求我们实现的是一个能够将ASCII码中数字转换成数字的函数,这个函数就是在c库函数中常用的atoi函数,这个函数将字符串转换成整型数字的时候有四点要求需注意:
该函数自动忽略字符串开始的空格符和制表符,直至遇到正负号或数字则开始转换。
当遇到正负号或数字后转换开始,这之后如果遇到非数字的字符则结束转换。
如果字符串开始不是空格符,制表符,正负号或者数字则函数的返回值为0
如果字符串所表示的数字超出了int数据所能表示的范围则当数字上溢时返回INT_MAX,下溢出时则返回INT_MIN。
根据这四个要求可以开始对字符串开始处理了,下列是我的c++代码,因为一开始没有考虑到一些情况所以到后来修改完成的代码有一些臃肿。
#include <string> #include <limits.h> #include <stdlib.h> #include <iostream> #include <math.h> using namespace std; class Solution { public: int myAtoi(string str) { int result = 0; string sresult; for(int i = 0; i < str.size(); i++) { if((str[i] == ' ' || str[i] == '\t') && sresult.size() == 0) { continue; } else { if ((str[i] == '+' || str[i] == '-') && sresult.size() == 0) { sresult.push_back(str[i]); } else if(str[i] <= '9' && str[i] >= '0') { sresult.push_back(str[i]); } else { break; } } } const int bitsOfInt = ((int)log10(INT_MAX)) + 1; if(sresult.size() == 0) { return 0; } else if(sresult.size() == 1) { if(sresult[0] == '+' || sresult[0] == '-') { return 0; } else { return sresult[0] - '0'; } } else { if(sresult[0] == '+' ) { if(sresult.size() > bitsOfInt + 1) { return INT_MAX; } for (int i = 1; i < sresult.size(); i++) { result = result * 10 + sresult[i] - '0'; if(result > INT_MAX / 10 && sresult.size() == bitsOfInt + 1 && i == sresult.size() - 2) { return INT_MAX; } if(result == INT_MAX / 10 && sresult.size() == bitsOfInt + 1) { if(sresult[i + 1] - '0' > INT_MAX - INT_MAX / 10 * 10) { return INT_MAX; } } } } else if(sresult[0] == '-') { if(sresult.size() > bitsOfInt + 1) { return INT_MIN; } for (int i = 1; i < sresult.size(); i++) { result = result * 10 - sresult[i] + '0'; if(result < INT_MIN / 10 && sresult.size() == bitsOfInt + 1 && i == sresult.size() - 2) { return INT_MIN; } if(result == INT_MIN / 10 && sresult.size() == bitsOfInt + 1) { if('0' - sresult[i + 1] < INT_MIN - INT_MIN / 10 * 10) { return INT_MIN; } } } } else { if(sresult.size() > bitsOfInt) { return INT_MAX; } for (int i = 0; i < sresult.size(); i++) { result = result * 10 + sresult[i] - '0'; if(result > INT_MAX / 10 && sresult.size() == bitsOfInt && i == sresult.size() - 2) { return INT_MAX; } if(result == INT_MAX / 10 && sresult.size() == bitsOfInt) { if(sresult[i + 1] - '0' > INT_MAX - INT_MAX / 10 * 10) { return INT_MAX; } } } } } return result; } };
相关文章推荐
- [leetcode] #8 String to Integer (atoi)
- Leetcode刷题记录 · No.8 String to Integer(atoi)
- [LeetCode]#8 String to Integer (atoi)
- 个人记录-LeetCode 8.String to Integer (atoi)
- [Leetcode] #8 String to Integer (atoi)
- leetcode String to Integer (atoi) java基本编程学习(二)
- #8 LeetCode——String to Integer (atoi)
- Leetcode练习 #8 String to Integer (atoi)
- LeetCode题解 #8 String to Integer (atoi)
- [LeetCode]#8 String to Integer (atoi)
- [Leetcode]#8 String to Integer (atoi)
- [LeetCode]String to Integer (atoi)
- 【LeetCode】8. String to Integer (atoi)
- leetcode 8. String to Integer (atoi)
- Leetcode 第8题 String to Integer (atoi)
- leetcode:String to Integer (atoi)
- 重拾编程之路--String to Integer (atoi)
- [JAVA]LeetCode8 String to Integer (atoi)
- LeetCode----8. String to Integer (atoi)
- leetcode String to Integer (atoi)