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

 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. POJ2549:Sumsets——题解

    http://poj.org/problem?id=2549 题目大意:从集合中找到四个不相同的数,满足a+b+c=d,输出最大的d. —————————————————————————— 该式子变为 ...

  2. responseBody注解可以直接返回字符串 && springMVC Controller get请求返回字符串是中文乱码解决

    //获取商品描述信息(html片段)字符串返回给前台填充html @RequestMapping(value="/item/desc/{itemId}",produces=Medi ...

  3. Change FZU - 2277 毒瘤啊 毒瘤题目

    There is a rooted tree with n nodes, number from 1-n. Root’s number is 1.Each node has a value ai. I ...

  4. bzoj 4900 [CTSC2017]密钥 模拟+乱搞

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4900 #include<cstring> #include<cmath&g ...

  5. synchronize 和volatile 实现共享变量在多线程中的可见性

    1.什么是线程可见性 可见性:一个线程对共享变量值的修改能够及时被其他线程看到. 共享变量:如果一个变量在多个线程工作内存中都存在副本,那么着给按量就是这几个线程的共享变量. 2.导致共享变量在线程间 ...

  6. Django1.5 自定义用户模型(总结)

    Django系统有自带的超级棒的认证机制,但是默认的用户模型有一点捉急,为了自定义用户模型,看了很多相关的文章,这里做一个总结: 你可以先看一下这篇,对Django的用户模型有一个比较全的了解: 来自 ...

  7. [LeetCode] 13. Roman to Integer ☆☆

    Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...

  8. redis 模糊查找keys

    Redis入门教程可参考:超强.超详细Redis数据库入门教程 Redis操作命令可参考:Redis操作命令总结 redis可以通过命令Keys Match来进行键值的模糊匹配,借助StackExch ...

  9. javascript 访问cookie信息

    在Javascript脚本里,一个cookie 实际就是一个字符串属性.当你读取cookie的值时,就得到一个字符串,里面当前WEB页使用的所有cookies的名称和值.每个cookie除了 name ...

  10. [CodeChef - GERALD07 ] Chef and Graph Queries

    Read problems statements in Mandarin Chineseand Russian. Problem Statement Chef has a undirected gra ...