MyAtoi

#include <iostream>
#include <cstring>

int MyAtoi(const char* str)
{
    if (str == NULL)
        return 0;

    int slen = strlen(str);
    if(slen < 0)
    {
        return 0;
    }
    const char* c = str;

    int ret = 0;
    int sign=1;
    while(*c == ‘ ‘)
    {
        c++;
    }
    if(*c == ‘+‘)
    {
        c++;
    }
    else if(*c == ‘-‘)
    {
        sign = -1;
        c++;
    }

    int t = 0;
    while((*c >=‘0‘)&&(*c <= ‘9‘))
    {
        t = *c-‘0‘;
        ret = ret *10+t;
        c++;
    }
    return ret * sign;
}

int main()
{
    std::cout << MyAtoi("+124") << std::endl;
    std::cout << MyAtoi("-124") << std::endl;
    std::cout << MyAtoi("    124") << std::endl;
    std::cout << MyAtoi("     +124") << std::endl;
    std::cout << MyAtoi("      -124") << std::endl;

    std::cout << MyAtoi(NULL) << std::endl;
    std::cout << MyAtoi("1d24") << std::endl;
    std::cout << MyAtoi("") << std::endl;

    std::cout << MyAtoi("124") << std::endl;

    return 0;
}

时间: 2024-11-08 22:20:22

MyAtoi的相关文章

LeetCode题解 #8 String to Integer (atoi)

又是一道恶心的简单题. 一开始没想到这么多情况的,幸好LeetCode是个很人性化的oj,能让你知道你在哪个case上错了,否则一辈子都过不了. 考虑不周到只能一个个补了. 列举一下恶心的case //" 010" //" +004500" //" -0012a42" //"2147483648" //" b11228552307" //"18446744073709551617" //

LeetCode:String to Integer (atoi)

1.题目名称 String to Integer (atoi) (字符串到数字的转换) 2.题目地址 https://leetcode.com/problems/string-to-integer-atoi/ 3.题目内容 英文:Implement atoi to convert a string to an integer. 中文:实现atoi函数,将输入的字符串(String类型)转换为整型数据(Integer类型) 提示:实现的atoi函数需要满足以下特征 忽略字符串第一个非空格字符前的所

8. String to Integer (atoi)

https://leetcode.com/problems/string-to-integer-atoi/description/ 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 poss

leetcode 8. String to Integer (atoi)

https://leetcode.com/problems/string-to-integer-atoi/ 题目:将字符串转换为整数. 规则: 1. 输入的字符串前可以有多个空格: 2.遇到'-'或者 '+'或者数字字符开始,为连续的多个数字字符.即遇到其他字符结束: 3. 多个连续数字字符后可以有其他的字符,但是不计算为整数: 4. 当输入为空,或者为不能转换的字符串,输出0: 5. 当整数大于2147483647,则输出2147483647.当小于-2147483648,则输出-214748

LeetCode8 String to Integer (atoi)

题意: Implement atoi to convert a string to an integer.  (Easy) 分析: 就是注意各种特殊情况,边界情况的判断,见代码注释. 1 class Solution { 2 public: 3 int myAtoi(string str) { 4 int start = 0; 5 long long result = 0; 6 int flag = 0; 7 //开头多余空格的处理 8 while (str[start] == ' ') { 9

[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

LeetCode 8

String to Integer (atoi) Implement atoi to convert a string to an integer. 1 /************************************************************************* 2 > File Name: LeetCode8.c 3 > Author: Juntaran 4 > Mail: [email protected] 5 > Created Tim

c++ 实现atoi()函数

1. 问题描写叙述 实现c++函数库中atoi()函数,要考虑到各种特殊情况: 空字符串. +和-号. 字符串前中后n个空格. 溢出. 非数字字符. 2. 解决方式 转换过程并不复杂.复杂的是要考虑到众多特殊情况. int myAtoi(string str) { if(str.length() == 0) return 0; //空串 bool isNeg = false; long re = 0; int i=0,cnt; for(;i<str.length(); i++) if(str[i

[Leetcode]leetcode1-10题随记

因为leetcode的题号会变动,所以先记录一下每一题对应的内容.   10 Regular Expression Matching 21.6% Hard   9 Palindrome Number 30.9% Easy   8 String to Integer (atoi) 13.3% Easy   7 Reverse Integer 23.6% Easy   6 ZigZag Conversion 23.3% Easy   5 Longest Palindromic Substring 2