点击打开题目链接

题目意思就是自己实现一个atoi函数,也就是将字符串转换成int型。

关于INT_MAX和INT_MIN, 只是在<limits.h>文件中定义的宏..分别是int型可以表示的最大值和最小值

还有就是定义大整数常量的时候,会出现这种警告:warning: this decimal constant is unsigned only in ISO C90

c的标准写道:

The C90 rule that the default type of a decimal integer constant is either int, long, or
unsigned long, depending on which type is large enough to hold the value without overflow,
simplifies the use of constants. The choices in C99 are int, long and long long.
C89 added the suffixes U and u to specify unsigned numbers. C99 adds LL to specify long
long.
Unlike decimal constants, octal and hexadecimal constants too large to be ints are typed as
unsigned int if within range of that type, since it is more likely that they represent bit
patterns or masks, which are generally best treated as unsigned, rather than “real” numbers.
Little support was expressed for the old practice of permitting the digits 8 and 9 in an octal
constant, so it was dropped in C89.
A proposal to add binary constants was rejected due to lack of precedent and insufficient utility.
Despite a concern that a “lower-case-l” could be taken for the numeral one at the end of a
numeric literal, the C89 Committee rejected proposals to remove this usage, primarily on the
grounds of sanctioning existing practice.

解决方式:

1 在常数后面增加一个UL标识,或者ULL表示,如4294967295UL,这样就不会报警了
2 使用十六进制的数字,如0xFFFFFFFF
3 使用gcc -std=c99 用99标准来编译

附上代码:

 class Solution {
public:
int atoi(const char *str) {
unsigned long long ans = ;
// is positive ?
int flag = ;
while (*str == ' ') str++;
if (*str == '+') {
str++;
} else if (*str == '-') {
flag = ;
str++;
}
while (*str != '\0') {
if ((*str) < '' || (*str) > '')
break;
ans = ans * + (*str) - '';
if (flag and ans > 2147483647ULL) {
return INT_MAX;
} else if (!flag and ans > 2147483648ULL) {
return INT_MIN;
}
str++;
} if (!flag) {
ans = -ans;
}
return (int)ans;
}
};

LeedCode OJ -- String to Integer (atoi)的更多相关文章

  1. 【LeedCode】String to integer(atoi)

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

  2. LeetCode OJ String to Integer (atoi) 字符串转数字

    #include <iostream> #include <assert.h> using namespace std; int ato(const char *str) { ...

  3. 【leetcode】String to Integer (atoi)

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

  4. No.008 String to Integer (atoi)

    8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...

  5. leetcode第八题 String to Integer (atoi) (java)

    String to Integer (atoi) time=272ms   accepted 需考虑各种可能出现的情况 public class Solution { public int atoi( ...

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

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

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

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

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

  9. LeetCode--No.008 String to Integer (atoi)

    8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...

随机推荐

  1. MyBatis-Spring(四)--MapperFactoryBean实现增删改查

    上一篇文章中提到,使用SqlSessionTemplat时需要输入一长串字符串来获取mapper,这种方式IDE不会检查程序的准确性并且很容易出错,所以这篇文章介绍另一种可以避免这种问题,并且也可以使 ...

  2. Web充斥着存在漏洞的过期JavaScript库

    虽然使用第三方软件库通常会降低开发的时间,但同时也会增加网站暴露出的攻击表面,对此我们应有充分的认识.因此需要保持第三方软件库的最新版本依赖,以便从安全更新中获益.即便如此,一份近期研究表明,在Ale ...

  3. valueStack(值栈)

    值栈 值栈(ValueStack)就是 OGNL 表达式存取数据的地方.在一个值栈中,封装了一次请求所需要的所有数据. 在使用 Struts2 的项目中,Struts2 会为每个请求创建一个新的值栈, ...

  4. C#icon图标文件转Image

    Icon icon = ICONHelper.GetFileIcon(filePath); MemoryStream mStream = new MemoryStream();//创建内存流 icon ...

  5. svn查看指定版本提交信息的命令

    通过svn命令查看指定版本提交的文件和日志信息 svn log -r ARG -v ARG :版本 可以是如下之一: NUMBER 版本号 '{' DATE '}' 在指定时间以后的版本 'HEAD' ...

  6. apache支持多主机头,并防止恶意空主机头的配置实现

    首先,需要启用 LoadModule vhost_alias_module modules/mod_vhost_alias.so # Virtual hostsInclude conf/extra/h ...

  7. host ngnix zull

    1.浏览器解析域名:www.baidu.com 2.由本地host解析得到IP:127.0.0.1 3.向IP传递请求,IP所在PC的Ngnix监听80端口. 4.IP所以PC收到请求后,nginx由 ...

  8. GUID(Globally Unique Identifier)全局唯一标识符

    最近有大量数据存入数据库时,因为主键为一个nvarchar类型,起初想着用int 类型,每次打开表的时候,获取最后一行的ID,然后让其++. 但发现由于字段是char类型,数据库对其进行了排序.再次插 ...

  9. linux 修改命令提示符&ubuntu目录颜色个性化

    1.linux 命令提示符个性化 在 ANSI 兼容终端(例如 xterm.rxvt.konsole 等)里, 可以用彩色显示文本而不仅仅是黑白. 本文示范了如何使用粗体和彩色的文字.     相信每 ...

  10. java并发系列(八)-----java异步编程

    同步计算与异步计算 从多个任务的角度来看,任务是可以串行执行的,也可以是并发执行的.从单个任务的角度来看,任务的执行方式可以是同步的,也可以是异步的. Runnable.Callable.Future ...