点击打开题目链接

题目意思就是自己实现一个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. win7安装mysql8提示one more product requirements have not been satisified

    点击否 然后查看一下到底缺啥,系统版本不一样,缺少的东西也不一定一样 去微软下就是了https://www.microsoft.com/en-us/download/details.aspx?id=4 ...

  2. 在PyCharm中导入Numpy和Pygame模块 (win8.1)

    我用的是anaconda安装python3.6 已经在终端 pip install numpy 但是在pycharm运行程序出现错误:ImportError: No module named nump ...

  3. LUOGU P4095 [HEOI2013]Eden 的新背包问题

    题目描述 " 寄 没 有 地 址 的 信 ,这 样 的 情 绪 有 种 距 离 ,你 放 着 谁 的 歌 曲 ,是 怎 样 的 心 情 . 能 不 能 说 给 我 听 ." 失忆的 ...

  4. Codeforces 222B 数组行列交换操作

    /*做完这题发现自己好水,太伤人了.... 不过还是学到一些,如果直接暴力模拟的话肯定是TLM.. 所以要用虚拟数组来分别保存当前数组的每行没列在初始数组中的位置...*/ #include<c ...

  5. 全球最牛的100家AI创企:有多少独角兽?

    全球最牛的100家AI创企:有多少独角兽? 自2012年以来,在共计263笔交易中筹集了38亿美元.那么,这100家顶尖AI公司主要集中在哪些方向?其中有多少家独角兽公司?中国企业表现如何呢? 近期, ...

  6. postman发送get和post请求

    一.postman发送get请求   在地址栏里输入请求url(用到拼接方式):http://127.0.0.1:8081/getuser?userid=1 选择“GET”方式, 点击“send”得到 ...

  7. drf作业01

    api\urls from django.conf.urls import url from . import views urlpatterns = [ url(r'^cars/$',views.C ...

  8. ubuntu和win10设置双显示器

    ubuntu:最右上角那个图标,点开找到系统设置,系统设置中找到“显示”中,在其中可以调节双屏显示或者只显示一个屏,图等会补... win10:现在是ubuntu系统所以操作忘记了写不出来,等下换系统 ...

  9. Leetcode16.3Sum Closest最接近的三数之和

    给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例如,给定数 ...

  10. CSS中各种居中方法

    CSS中各种居中方法,本文回顾一下,便于后续的使用. 水平居中方法 1.行内元素居中 行内元素居中是只针对行内元素的,比如文本(text).图片(img).按钮等行内元素,可通过给父元素设置 text ...