将字符串转化为数字,其注意事项有:

Requirements for atoi:

The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.

The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.(出现数字之后的字母直接忽略并结束,哪怕后面还有数字)

If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.

If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.

INT_MAX,INT_MIN 是一个常量,分别代表INT所能表示的最大值和最小值。也可以使用模板类numeric_limits<int>::max()和numeric_limits<int>::min(),这个需要包含头文件#include<limits>
使用int,倘若某个数超过了2147483647则会变为负数,反过来一样
 
在调试过程中发现:使用int         监视变量的实际转化: (214748364*10+8=-2147483648)
                                                            214748364*10+9=-2147483647
我对此的解决方案为:采用long long类型来存储:
#include<iostream>
#include<string>
#include<cmath>
//#include<limits>
using namespace std; class Solution {
public:
int myAtoi(string str) {
long long tes, res = ;
int sign = , count = ;
bool flag = false;
int len = str.length();
for (int i = ; i < len; ++i)
{
//首先就是去除最前面连续着的所有空格
if (str[i] == ' '&&flag == false)
continue;
else
flag = true;
//符号不能+-都出现
if (str[i] == '+' || str[i] == '-')
count++;
//最后的结果的正负
if (str[i] == '-')
{
sign = -;
}
//一旦中间遇到字母或者空格就结束了
if (str[i] >= 'a'&&str[i] <= 'z' || str[i] >= 'A'&&str[i] <= 'z' || str[i] == ' ')
break;
//真正干活的,将字符形式的数字转化为真正的数字 if (str[i] >= ''&&str[i] <= '')
{
int s = str[i] - '';
res = res * + s;
tes = res*sign;
if (tes > INT_MAX)
return INT_MAX;
else if (tes < INT_MIN)
return INT_MIN;
}
}
if (count>)
return ;
return sign*res;
}
}; int main()
{
Solution test;
string s1 = "";
string s2 = " -11919730356x";
int result = test.myAtoi(s1);
cout << result << endl;
result = test.myAtoi(s2);
cout << result << endl; //long long temp = 2147483647;
//int r = temp;
//cout << r;
////long long temp=-12345678901;
////if (temp < (long long)INT_MIN)
//// cout << INT_MIN;
return 0;
}
 
 
 

atoi (String to Integer) leetcode的更多相关文章

  1. ElasticSearch 5学习(9)——映射和分析(string类型废弃)

    在ElasticSearch中,存入文档的内容类似于传统数据每个字段一样,都会有一个指定的属性,为了能够把日期字段处理成日期,把数字字段处理成数字,把字符串字段处理成字符串值,Elasticsearc ...

  2. Java基础:整型数组(int[]、Integer[])排序

    Windows 10家庭中文版,java version "1.8.0_152",Eclipse Oxygen.1a Release (4.7.1a), 参考链接:http://w ...

  3. 自动拆装箱(int,Integer)

    包装类型Java语言是一个面向对象的语言,但是Java中的基本数据类型却是不面向对象的,这在实际使用时存在很多的不便,为了解决这个不足,在设计类时为每个基本数据类型设计了一个对应的类进行代表,这样八个 ...

  4. Find the duplicate Number (鸽巢原理) leetcode java

    问题描述: Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive ...

  5. 浅谈 Java 字符串(String, StringBuffer, StringBuilder)

    我们先要记住三者的特征: String 字符串常量 StringBuffer 字符串变量(线程安全) StringBuilder 字符串变量(非线程安全) 一.定义 查看 API 会发现,String ...

  6. 02_Redis数据类型(String、Hash)

    [Redis数据类型] redis是通过key-Value来存储的,其支持的数据类型如下: 1.字符串 2.Hash 3.List 4.Set 5.SortSet(zset) 注:redis中,命令( ...

  7. java字符串(String和StringBuilder)

    1.String 1.1.创建String对象的方法(三种方式) String s1 = "zhang"; 创建一个字符串对象zhang,名为s1 String s2 = new ...

  8. LeetCode 8. String to Integer (atoi) (字符串到整数)

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  9. C#LeetCode刷题之#13-罗马数字转整数(Roman to Integer)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3842 访问. 罗马数字包含以下七种字符: I, V, X, L, ...

随机推荐

  1. zabbix3.4自定义监控

    zabbix的服务器.客户端都已经部署完成,监控正常,用的是微信报警: 现在想监控一台Linux服务器(172.16.0.56)的剩余内存,在小于一定值的时候就报警: 1.在172.16.0.56上, ...

  2. 骨骼蒙皮动画(SkinnedMesh)的原理解析(一)

    http://blog.csdn.net/jimoshuicao/article/details/9253999 一)3D模型动画基本原理和分类 3D模型动画的基本原理是让模型中各顶点的位置随时间变化 ...

  3. 3DMAX 8 角色建模2 身体

    身体 20 25 60 分段 3 2 3 位置直接设置到-12.5 0 0,删除内部面,实例镜像出另一半x偏移25 1 身体大体形状 ,,,, 2 背阔肌 , 3腹肌 4 臀部 ,, 5 肩部 调整点 ...

  4. bzoj 4259 4259: 残缺的字符串【FFT】

    和bzoj 4503 https://www.cnblogs.com/lokiii/p/10032311.html 差不多,就是再乘上一个原串字符 有点卡常,先在点值下算最后一起IDFT #inclu ...

  5. 【HDU - 1257】最少拦截系统(贪心)

    最少拦截系统 Descriptions: 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能超过前一发的 ...

  6. scrapy 连接错误

    twisted.python.failure.failure twisted.internet.error.connectionlost: connection to the other side w ...

  7. Spring + MyBaits java.lang.reflect.InvocationTargetException 启动日志报错

    调试发现 实例化 class org.apache.ibatis.logging.slf4j.Slf4jImpl时发生异常,所以 slf4j jar 问题解决: http://www.cnblogs. ...

  8. 【数据结构(C语言版)系列四】 串

    串类型的定义 串(或字符串)是由零个或多个字符组成的有限序列,一般记为 s = 'a1a2...an',s为串名.子串在主串中的位置以子串的第一个字符在主串中的位置来表示. 串和表示和实现——定长顺序 ...

  9. POM报错Failure to transfer org.apache.maven.plugins:maven-resources-plugin:pom:2.6 from

    解决方式一:   1.查看.m2\repository\org\apache\maven\plugins\maven-resources-plugin\下maven-resources-plugin- ...

  10. Closures闭包

    //: Playground - noun: a place where people can play import UIKit /* 闭包 Swift ====> Closures Ruby ...