点击打开题目链接

题目意思就是自己实现一个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. phpStrom编辑器 通过 git 提交代码到 gitlab

    前提: 1.已经成功安装 git: 2.将 phpstrom 和 gitlab 连接起来.参考此文章 一.在 phpstrom 中打开需要推送的项目 二.将 ‘工作区’ 代码 添加到 ‘暂存区’ 三. ...

  2. MySQL时间格式转换

    1.时间转换成特定字符串 例:select DATE_FORMAT(now(),'%Y-%m-%d %H:%i::%s'); --> '2019-10-16 10:59::18' 2.一种字符串 ...

  3. 第15章 RMAN备份 ​

    第15章 RMAN备份 oracle推荐的备份工具是rman(恢复管理器:recovery manager),用操作系统命令执行的备份被称为用户管理的备份.使用rman执行的备份被称为服务器管理备份. ...

  4. Vue. 之 刷新当前页面,重载页面数据

    Vue. 之 刷新当前页面,重载页面数据 如下截图,点击左侧不同的数据,右侧根据左侧的KEY动态加载数据.由于右侧是同一个页面,在进行路由跳转后,不会再次刷新数据. 解决方案: 右侧的页面中 scri ...

  5. JasperReport生命周期3

    JasperReports的主要目的是为了在一个简单而灵活的方式创建页面为导向,准备好打印文档.下面的流程图描述了一个典型的工作流程,同时创建报表. 如在图片的生命周期具有以下明显的阶段 设计报表在这 ...

  6. 只要三步!阿里云DLA帮你处理海量JSON数据

    概述 您可能有大量应用程序产生的JSON数据,您可能需要对这些JSON数据进行整理,去除不想要的字段,或者只保留想要的字段,或者仅仅是进行数据查询. 那么,利用阿里云Data Lake Analyti ...

  7. 威胁快报|ProtonMiner挖矿蠕虫扩大攻击面,加速传播

    背景 近日,阿里云安全监测到一种挖矿蠕虫,正在互联网上加速传播.阿里云安全根据它使用ProtonMail邮箱地址作为矿池用户名的行为,将其命名为ProtonMiner.据分析,这种蠕虫与TrendMi ...

  8. TZ_13_Eureka的高可用

    1.Eureka Server即服务的注册中心,在刚才上一篇中,我们只有一个EurekaServer,事实上EurekaServer也可以是一个集群,形成高可用的Eureka中心. 目的:多个Eure ...

  9. Spring的IoC容器(转)BeanFactory

    Spring的IoC容器 Spring读书笔记-----Spring的Bean之Bean的基本概念 加菲猫 Just have a little faith. Spring的IoC容器 (用户持久化类 ...

  10. mysql创建数据库指定utf8编码

    CREATE DATABASE IF NOT EXISTS dbname DEFAULT CHARSET utf8;