点击打开题目链接

题目意思就是自己实现一个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. devc++读取不了当前目录下的文件

    devc++在当前目录新建了一个文件之后,用文件读取的操作报错:     如图所示:           解决方案: 先把该文件从左侧工作空间中移除:       移除之后就没了:         再 ...

  2. 数据库访问技术 odbc dao rdo uda jet oledb

    一.UDA(UniversalDataAccess) 这是微软提供的通用数据访问策略.包括ADO.OLEDB和ODBC.它不光提供了数据库的访 问能力,对于其它的数据存储技术也同样支持,如目录服务.E ...

  3. pytorch 多GPU训练过程中出现ap=0情况

    原因可能是pytorch 自带的BN bug:安装nvidia apex 可以解决: $ git clone https://github.com/NVIDIA/apex $ cd apex $ pi ...

  4. Java数据结构和算法(八)--红黑树与2-3树

    红黑树规则: 1.每个节点要么是红色,要么是黑色 2.根节点都是黑色节点 3.每个叶节点是黑色节点 3.每个红色节点的两个子节点都是黑色节点,反之,不做要求,换句话说就是不能有连续两个红色节点 4.从 ...

  5. 【html、CSS、javascript-2】CSS基础

    CSS CSS是Cascading Style Sheets的简称,中文称为层叠样式表,用来控制网页数据的表现,可以使网页的表现与数据内容分离. 一 css的四种引入方式 1.行内式          ...

  6. #queue队列 #生产者消费者模型

    #queue队列 #生产者消费者模型 #queue队列 #有顺序的容器 #程序解耦 #提高运行效率 #class queue.Queue(maxsize=0) #先入先出 #class queue.L ...

  7. loj6229 这是一道简单的数学题

    https://loj.ac/problem/6229 题解:https://blog.csdn.net/Vectorxj/article/details/79094659 套路推式子,杜教筛,证明复 ...

  8. laravel-admin列表排序在使用了$grid->model()->latest()后$grid其它加上sortable()可排序的列在排序时不起作用

    laravel-admin这个基于laravel的后台框架,简单易用,$grid的默认排序是主键升序的排列方式,但在使用了`$grid->model()->latest();`自定义默认排 ...

  9. .net4.6版本前设置window子窗口位置主窗口闪烁

    在安装了.net4.6的版本是不会出现该问题的,但是在4.6以下的版本会出现,当设置之窗体的left和top属性时,会让主窗体闪烁一下. 之前是在load事件下写的: child_window.loa ...

  10. Django ORM中的查询,删除,更新操作

    ORM查询操作 修改views.py文件 from django.shortcuts import render, HttpResponse from app01 import models from ...