Skip to content

Instantly share code, notes, and snippets.

@y-fedorov
Last active June 13, 2018 14:58
Show Gist options
  • Select an option

  • Save y-fedorov/3ba9b701b172c157043c454d8777e063 to your computer and use it in GitHub Desktop.

Select an option

Save y-fedorov/3ba9b701b172c157043c454d8777e063 to your computer and use it in GitHub Desktop.
8. String to Integer (atoi)
/*
Implement atoi which converts a string to an integer.
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.
Note:
Only the space character ' ' is considered as whitespace character.
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned.
Copyright © 2018 LeetCode
*/
#include <iostream>
#include <functional>
#include <cmath>
#include <cctype>
#include <algorithm>
int myAtoi(std::string str) {
//trim L
str.erase(str.begin(), std::find_if(str.begin(), str.end(), [](int ch) {
return !std::isspace(ch);
}));
//trim R
str.erase(std::find_if(str.rbegin(), str.rend(), [](int ch) {
return !std::isspace(ch);
}).base(), str.end());
auto stringSize = str.length();
if (stringSize == 0)
return 0;
int64_t value = 0;
bool minusMode = false;
bool gotValue = false;
size_t initValue = 0;
auto &firstChar = str.at(0);
if (firstChar == '+') {
initValue++;
}
if (firstChar == '-') {
minusMode = true;
initValue++;
}
for (auto i = initValue; i < stringSize; i++) {
auto &c = str.at(i);
bool isDigit = std::isdigit(c) != 0;
if (isDigit) {
auto digit = c - 0x30;
value = value * 10 + digit;
if (value > INT_MAX && minusMode) {
return INT_MIN;
}
if (value >= INT_MAX) {
value = INT_MAX;
break;
}
gotValue = true;
continue;
}
if (gotValue)
break;
return 0;
}
return static_cast<int>(minusMode ? value * (-1) : value);
}
void checkValue(std::string input, int32_t expected)
{
auto result = myAtoi(input);
if (result != expected) {
throw new std::runtime_error("Failed on \"" + input + "\" Expected: " + (expected ? "(True)" : "(False)"));
}
}
int main()
{
// Defaults from task
checkValue("42", 42);
checkValue(" -42", -42);
checkValue("4193 with words", 4193);
checkValue("words and 987", 0);
checkValue("-91283472332", INT_MIN);
//Additional checks
checkValue("+-2", 0);
checkValue("-", 0);
checkValue("+", 0);
checkValue("2147483648", 2147483647);
checkValue("-2147483647", -2147483647);
checkValue("2147483648", 2147483647);
std::cout << "Success!" << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment