字符串转为数字,细节题。要考虑空格、正负号,当转化的数字超过最大或最小是怎么办。

 int atoi(char *str)
{
int len = strlen(str);
int sign = ;
int num = ; int i = ;
while (str[i] == ' '&& i < len)i++; if (str[i] == '+')i++;
else if (str[i] == '-')
{
sign = -;
i++;
} for (; i < len; i++)
{
if (str[i]<''&&str[i]>'')break;
if (num>INT_MAX / || (num==INT_MAX / && (str[i] - '')>INT_MAX % ))
{
return sign == - ? INT_MIN : INT_MAX;
}
num = num * + str[i] - '';
} return num*sign;
}

eetcode 之String to Integer (atoi)(28)的更多相关文章

  1. 【leetcode】String to Integer (atoi)

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

  2. No.008 String to Integer (atoi)

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

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

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

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

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

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

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

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

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

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

  8. leetcode-algorithms-8 String to Integer (atoi)

    leetcode-algorithms-8 String to Integer (atoi) Implement atoi which converts a string to an integer. ...

  9. LeetCode: String to Integer (atoi) 解题报告

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

随机推荐

  1. 《Java程序设计》第六周学习总结 20165218 2017-2018-1

    20165218 2017-2018-1 <Java程序设计>第六周学习总结 教材学习内容总结 第8章 常用实用类 String类 不可以有子类 在java.lang 包中,被默认引入 S ...

  2. HDU 1002 (高精度加法运算)

    A + B ProblemII Time Limit: 2000/1000 MS(Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  3. webpack优化总结

    1. 分包 将不需要变动的第三方包分离出去, 主要方法有: (1). externals(2). DllPlugin(3). expose-loader(4). ProviderPlugin 2. 拆 ...

  4. 5028: 小Z的加油店(线段树)

    NOI2012魔幻棋盘弱化版 gcd(a,b,c,d,e)=gcd(a,b-a,c-b,d-c,e-d) 然后就可以把区间修改变成差分后的点修了. 用BIT维护原序列,线段树维护区间gcd,支持点修区 ...

  5. HTTP的消息结构?

    参考:http://www.runoob.com/http/http-messages.html (1)请求数据包结构: 第一部分:请求行(数据包的第一行内容)[GET/HTTP/1.1] 请求行包含 ...

  6. bzoj 2654 tree 二分+kruskal

    tree Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 2739  Solved: 1126[Submit][Status][Discuss] Des ...

  7. Moodle插件开发——Blocks(版块)

    前提: 1)     基于Moodle3.0,要求Moodle版本高于2.0 2)     PHP编程基础:语言的了解和开发工具使用 有经验的开发人员和那些只是想程序员的参考文本应参阅附录A. 1.  ...

  8. [Android篇]Android Studio + Genymotion 一夜无眠 ,超级详细版本[请使用新版2.0]

    环境说明:这里很重要,因为我在windows10中吃过很多的亏 操作系统: windows 7 64位系统 JDK 64位 : jdk1.7.0_75 注意我这里吃过亏!都用64位的! Android ...

  9. github访问速度慢,样式加载不完全解决

    现象如下图: 解决方案: 绑定host 185.31.17.184 github.global.ssl.fastly.net

  10. 【C++ STL】Deques

    1.结构 容器deque和vector非常相似,也是采用动态数组来管理元素,提供随机存取,有着和vector几乎一样的接口,不同的是deque的动态数组头尾都开放,因此可以在头尾都可以进行快速的安插和 ...