LeetCode 008
2016-04-24 23:47
190 查看
String to Integer (atoi)
Implement atoi to convert a string to an integer.
Implement atoi to convert a string to an integer.
/************************************************************************* > File Name: LeetCode8.c > Author: Juntaran > Mail: Jacinthmail@gmail.com > Created Time: 2016年04月24日 星期日 15时51分05秒 ************************************************************************/ /************************************************************************* Implement atoi to convert a string to an integer. ************************************************************************/ #include <stdio.h> #include <limits.h> int myAtoi(char* str) { int flag = 1; long sum = 0; while( *str == ' ' ){ str++; } if ( *str == '+' || *str == '-' ){ flag = (*str++ == '+' ? 1 : -1 ); } while( isdigit(*str) && sum < INT_MAX ){ sum = 10*sum + (*str++ - '0'); } if( flag == 1 ){ sum = sum > INT_MAX ? INT_MAX : sum; printf("%d\n",sum); return sum; }else{ sum = (sum *= flag) < INT_MIN ? INT_MIN : sum; printf("%d\n",sum); return sum; } } int main(){ char* str = "-100.ab"; myAtoi(str); }
相关文章推荐
- 翁恺老师零基础学JAVA语言第五周数组--投票程序例题-个人理解
- JAVA中的File类,I/O流基础
- iOS开发系列--Objective-C之类和对象
- dup and dup2的剖析
- 编程珠玑(第一章(2):位图索引)
- 服务器之新手入门
- 虚幻4 渲染staticmesh过程。
- LeetCode 007
- leetcode刷题,总结,记录,备忘 201
- 进行iOS代码调试之LLDB调试器
- 2016腾讯de笔试online
- 20145216史婧瑶《Java程序设计》第8周学习总结
- Log打印日志遇到的问题
- [spring源码学习]二、IOC源码——配置文件读取
- 20145303《Java程序设计》实验三实验报告
- OSI七层模型与TCP/IP五层模型——以太网基础01
- Android 网络编程(一)Http网络请求详解?
- 翁恺老师零基础学JAVA语言第五周数组--求平均数例题-个人理解
- 判断 a[i]是否是字母或数字的函数
- Windows 7 Professional Product Key Free for You