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

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. Bootstrap-CSS:表单

    ylbtech-Bootstrap-CSS:表单 1.返回顶部 1. Bootstrap 表单 在本章中,我们将学习如何使用 Bootstrap 创建表单.Bootstrap 通过一些简单的 HTML ...

  2. margin-----总结----解析逻辑

    margin的解析逻辑 在 margin 中 top.right.bottom.left 的参考线并不一致为一类,而是分为了两类参考线,top 和 left 的参考线属于一类,right 和botto ...

  3. bzoj4556

    后缀自动机+二分+倍增+线段树合并 后缀自动机真好用 后面一个串是固定的,那么我们要对前面的串进行一些操作.我们想既然是求lcp,那么我们得先翻转原串,这样前缀变成了后缀,然后二分一下,从d在自动机上 ...

  4. std::unique

    类属性算法unique的作用是从输入序列中“删除”所有相邻的重复元素. 该算法删除相邻的重复元素,然后重新排列输入范围内的元素,并且返回一个迭代器(容器的长度没变,只是元素顺序改变了),表示无重复的值 ...

  5. UVaLive 6591 && Gym 100299L Bus (水题)

    题意:略. 析:不解释,水题. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include < ...

  6. (水题)洛谷 - P1579 - 哥德巴赫猜想(升级版)

    https://www.luogu.org/problemnew/show/P1579 先预处理出素数看看有多少个,大概才2500个不到(事实上素数的个数大约是 $\frac{n}{ln(n)}$ ) ...

  7. Unity3D将来时:IL2CPP(上)

    http://inpla.net/thread-8197-1-1.html Unity3D将来时:IL2CPP(上) (注:本文详细的讲述了C#,Mono,.Net, IL等Unity使用到的概念,如 ...

  8. 基于.NetCore和ABP框架如何让Windows服务执行Quartz定时作业

    demo地址:ABP.WindowsService 该系列文章启发自 How to: Create a Windows Service that schedules jobs, logs and is ...

  9. windows 异步通知I/O模型与重叠I/O模型

    一.异步IO模型(asynchronous IO) (1)什么是异步I/O 异步I/O(asynchronous I/O)由POSIX规范定义.演变成当前POSIX规范的各种早起标准所定义的实时函数中 ...

  10. 微信BUG之微信内置的浏览器中window.location.href 不跳转

    最近做微信开发遇到这个问题,查了一些文档,总结一下 1.url后面加参数 indow.location.href = url +'?timestamp='+ new Date().getTime()+ ...