Leetcode 8 String to Integer (atoi) 字符串处理
题意:将字符串转化成数字。
前置有空格,同时有正负号,数字有可能会溢出,这里用long long解决(leetcode用的是g++编译器),这题还是很有难度的。
class Solution {
public:
int myAtoi(string str) {
int sign = ,i = ;
for(;i<str.size() && str[i] == ' '; ++i);
char ss[] ="+-" ;
int sn[] ={,-};
for(int j =;j<;++j){
if(str[i] == ss[j]) {
sign *= sn[j];
i++;
break;
}
}
long long ans = ;
for (; i<str.size(); ++i)
{
if(isdigit(str[i])){
ans = *ans + str[i] - '';
if(sign == && ans > (long long)INT_MAX){
ans = (long long)INT_MAX;break;
}
else if(sign == - && ans> -(long long)INT_MIN){
ans = -(long long)INT_MIN;break;
}
}
else break;
}
return ans * sign;
}
};
Leetcode 8 String to Integer (atoi) 字符串处理的更多相关文章
- [LeetCode] 8. String to Integer (atoi) 字符串转为整数
Implement atoi which converts a string to an integer. The function first discards as many whitespace ...
- [leetcode]8. String to Integer (atoi)字符串转整数
Implement atoi which converts a string to an integer. The function first discards as many whitespace ...
- 【LeetCode】String to Integer (atoi)(字符串转换整数 (atoi))
这道题是LeetCode里的第8道题. 题目要求: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们 ...
- LeetCode OJ String to Integer (atoi) 字符串转数字
#include <iostream> #include <assert.h> using namespace std; int ato(const char *str) { ...
- Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)
Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...
- 【leetcode】String to Integer (atoi)
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- leetcode day6 -- String to Integer (atoi) && 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 ...
- String to Integer (atoi) - 字符串转为整形,atoi 函数(Java )
String to Integer (atoi) Implement atoi to convert a string to an integer. [函数说明]atoi() 函数会扫描 str 字符 ...
- [leetcode] 8. String to Integer (atoi) (Medium)
实现字符串转整形数字 遵循几个规则: 1. 函数首先丢弃尽可能多的空格字符,直到找到第一个非空格字符. 2. 此时取初始加号或减号. 3. 后面跟着尽可能多的数字,并将它们解释为一个数值. 4. 字符 ...
随机推荐
- BDC批导数据
1.输入事务代码SHBD进入以下界面: 点击新建记录,创建一个新的BDC录屏记录, 然后根据记录条件进行 BDC录屏代码 perform fill_bdc using ANLKL. call tran ...
- Spring配置数据源的几种形式
Spring中提供了4种不同形式的数据源配置方式: 1.Spring自带的数据源(DriverMangerDataSource); 2.DBCP数据源; 3.C3P0数据源; 4.JNDI数据源. 以 ...
- python datatime
一.datetime 1.date date.today() 2.time 3.datetime datetime.now() datetime.strftime(fmt) 转换为字符串 dateti ...
- zz---Tomcat服务器下部署项目几种方式
http://blog.sina.com.cn/s/blog_550281c60101hvrs.html 一.静态部署1.直接将web项目文件件拷贝到webapps 目录中 Tomcat的We ...
- 烧写AT91Bootstrap不能连接SAM-BA的解决方法
AT91与SAM-BA的连接是由于芯片内有一段固化的代码运行起来后才会检测到目标板并建立连接. 假设现在你烧写了Bootstrap进去,芯片上电后发现有可运行的代码,从而就不执行片内固化的那个代 ...
- PHP裁剪图片并上传完整demo
日前根据功能需求,要做一个图片裁剪上传的功能,在网上找了好久,找到了这位仁兄写的demo! 下载压缩包
- 新浪微博SDK的使用
花了两天时间研究了一下新浪微博SDK,遇到了不少问题,有必要整理一下 1.首先下载下weiboSdk,导入weiboSDKD和weiboSDKDemo两个项目,这时发现导入的weiboSDKDemo项 ...
- Mysql --分区(3)range分区
3.分区类型 RANGE分区 按照range分区的表是利用取值范围将数据分成分区,区间要连续并且不能互相重叠,使用values less than操作符进行分区定义 CREATE TABLE tnp ...
- SECHS
题目描述 对于给定的正整数N,我们把[1, N]中的整数按照字符串的字典序排序得到N 项数列A(N). 例如,N = 11的时候,A(N) = {1, 10, 11, 2, 3, 4, 5, 6, 7 ...
- [Xamarin] 關於SQLite 的操作 (转帖)
我們聊一下常用的東西,SQLite,這東西很常用到,當再寫手機APP的時候,有時候會在Client 做 cache或是做一些資料的管理都很必須會用到,我們來看看今天的範例 建立SQL Lite 資料庫 ...