博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode]String to Integer (atoi)
阅读量:4150 次
发布时间:2019-05-25

本文共 1368 字,大约阅读时间需要 4 分钟。

class Solution {//note://1. NULL//2. sign//3. prefix ' '//4. out of range, to avoid this we use long longpublic:	int atoi(const char *str) {		// Start typing your C/C++ solution below		// DO NOT write int main() function		if(str == NULL) return 0;//NULL		while (*str == ' ')//prefix ' ' 			str++;		int sign = 1;		if(*str == '-')//sign '+' or '-'		{			sign = -1;			str++;		}		else if(*str == '+')			str++;		long long ans = 0;		while (*str >= '0' && *str <= '9')		{			ans = ans*10+*str-'0';			if(ans > INT_MAX)//out of range				return sign < 0 ? INT_MIN : INT_MAX;			str++;		}		ans *= sign;		return (int)ans;	}};

second time

class Solution {public:    int atoi(const char *str) {        // Start typing your C/C++ solution below        // DO NOT write int main() function        if(str == NULL) return 0;                bool sign = true;                while (*str == ' ')//prefix ' '               str++;                      if(*str == '+' || *str == '-')        {            if(*str == '+') sign = true;            else sign = false;            str++;        }                 long long ans = 0;        while(*str >= '0' && *str <= '9')        {            ans = ans*10+(*str-'0');            if(ans > INT_MAX) return sign == true ? INT_MAX : INT_MIN;            str++;        }                if(sign == false) return (int)(-ans);        else return (int)ans;    }};

转载地址:http://qqxti.baihongyu.com/

你可能感兴趣的文章
【JavaScript 教程】标准库—Date 对象
查看>>
前阿里手淘前端负责人@winter:前端人如何保持竞争力?
查看>>
【JavaScript 教程】面向对象编程——实例对象与 new 命令
查看>>
我在网易做了6年前端,想给求职者4条建议
查看>>
SQL1015N The database is in an inconsistent state. SQLSTATE=55025
查看>>
RQP-DEF-0177
查看>>
MySQL字段类型的选择与MySQL的查询效率
查看>>
Java的Properties配置文件用法【续】
查看>>
JAVA操作properties文件的代码实例
查看>>
java杂记
查看>>
RunTime.getRuntime().exec()
查看>>
Oracle 分组排序函数
查看>>
删除weblogic 域
查看>>
VMware Workstation 14中文破解版下载(附密钥)(笔记)
查看>>
日志框架学习
查看>>
日志框架学习2
查看>>
SVN-无法查看log,提示Want to go offline,时间显示1970问题,error主要是 url中 有一层的中文进行了2次encode
查看>>
NGINX
查看>>
Qt文件夹选择对话框
查看>>
DeepLearning tutorial(5)CNN卷积神经网络应用于人脸识别(详细流程+代码实现)
查看>>