enum status{VALID = , INVALID};
int g_status; long long SubStrToInt(const char* str, bool minus)
{
long long num = ;
int flag = minus ? - : ; while (*str != '\0') {
if (*str >= '' && *str <= '') {
num = num * + flag * (*str - ''); if ((!minus && num > 0x7FFFFFFF)
|| (minus && num < (signed int)0x80000000)) {
num = ;
break;
}
str++;
} else {
num = ;
break;
}
}
if (*str == '\0')
g_status = VALID; return num;
} int StrToInt(const char* str)
{
g_status = INVALID;
long long num = ;
bool minus = false; if (str != NULL && *str != '\0') {
if (*str == '+') {
str++;
}
else if (*str == '-') {
minus = true;
str++;
} if (*str != '\0')
num = SubStrToInt(str, minus);
}
return (int)num;
}

需要考虑的几个方面:

1、输入的字符串表示正数、负数和0;

2、边界值,最大的正整数和最小的负整数;

3、输入字符串为NULL、空字符串、字符串中有非数字、字符串开始的一段有空格等。

【LeetCode 8_字符串_实现】String to Integer (atoi)的更多相关文章

  1. 【LeetCode每天一题】String to Integer (atoi)(字符串转换成数字)

    Implement atoi which converts a string to an integer.The function first discards as many whitespace ...

  2. 【Leetcode】【Easy】String to Integer (atoi)

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  3. 【leetcode刷题笔记】String to Integer (atoi)

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  4. Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)

    Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...

  5. LeetCode 8. 字符串转换整数 (atoi)(String to Integer (atoi))

    8. 字符串转换整数 (atoi) 8. String to Integer (atoi) 题目描述 LeetCode LeetCode8. String to Integer (atoi)中等 Ja ...

  6. 【leetcode】String to Integer (atoi)

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

  7. 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 ...

  8. String to Integer (atoi) - 字符串转为整形,atoi 函数(Java )

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

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

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

随机推荐

  1. 商铺项目(使用DES加密配置信息)

    package com.ouyan.o2o.util; import java.security.Key; import java.security.SecureRandom; import java ...

  2. Keras常用层

    Dense层:全连接层 Activatiion层:激活层,对一个层的输出施加激活函数 Dropout层:为输入数据施加Dropout.Dropout将在训练过程中每次更新参数时按一定概率(rate)随 ...

  3. mysql 约束条件 unique key 唯一的键

    如果不设置unique 会出现两条相同的记录 mysql)); Query OK, rows affected (0.01 sec) mysql ,,'mike'); Query OK, rows a ...

  4. MongDb的安装

    MongoDB是一个基于分布式文件存储的数据库,由c++语言编写,为WEB应用提供可扩展的高性能数据存储解决方案.MongoDB属于非关系数据库,也不能说完全属于,更像是介于关系数据库和非关系数据库之 ...

  5. python模块之shutil高级文件操作

    简介 shutil模块提供了大量的文件的高级操作.特别针对文件拷贝和删除,主要功能为目录和文件操作以及压缩操作.对单个文件的操作也可参见os模块. 注意即便是更高级别的文件复制函数(shutil.co ...

  6. onvif协议client与server对接

    happytimesoft有完整的c语言开发的onvif client和server,一共1000$,真便宜,haha. http://www.happytimesoft.com/products/m ...

  7. centos7 lua安装

    yum -y install gcc automake autoconf libtool makeyum install readline-develcurl -R -O http://www.lua ...

  8. http之url和uri

    HTTP使用统一资源标识符(Uniform Resource Identifiers, URI)来传输数据和建立连接.URL是一种特殊类型的URI,包含了用于查找某个资源的足够的信息 URL,全称是U ...

  9. A题:Common Substrings(KMP应用)

    原题链接 注意:2号和3号get_next()函数中next[i]赋值时的区别,一个是0,一个是1,且不能互换 #include<cstdio> #include<cstring&g ...

  10. 《FontForge常见问题FAQ》字王翻译版

    <FontForge常见问题FAQ> 字王翻译版 原文: http://fontforge.github.io/en-US/faq/ 翻译: 字王·中国   blog: http://bl ...