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

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. UI:一个IOS工程的标准框架

    来自cocachina 的写法 参考 声明:本文来自互联网,非本人原创,仅供参考学习使用. 我的iOS工程结构 接下来,我就简单介绍下我做iOS项目时使用的工程结构.首先要说的是,这只是我的工程结构, ...

  2. 10.22~10.28一周经典题目整理(meeting,BZOJ4377,POJ3659)

    meeting:给正n边形每个点染上黑色或者白色,问有多少个同色的等腰三角形. 以正五边形为例这里将最上面的点作为顶点,得到若干对相等的腰 ,注意到以最上面的点作为顶点的等腰三角形的个数,等于颜色相等 ...

  3. redis简介、安装、配置和数据类型

    redis简介.安装.配置和数据类型 redis简介 Redis是一个开源(BSD许可),内存存储的数据结构服务器,可用作数据库,高速缓存和消息队列代理. 它支持字符串.哈希表.列表.集合.有序集合, ...

  4. 任务37:生成 JWT Token

    实现给用户办法token 默认是可以访问valuesController的,返回的状态是200 http://localhost:5429/api/values 返回的状态码是200 把ValuesC ...

  5. python 导入.py

    转自: https://blog.csdn.net/winycg/article/details/78512300 在同一个文件夹下调用函数:A.py文件: def add(x,y): print(' ...

  6. Django models数据库配置以及多数据库调用设置

    今天来说说web框架Django怎么配置使用数据库,也就是传说中MVC(Model View Controller)中的M,Model(模型). 简单介绍一下Django中的MVC: 模型(model ...

  7. 洛谷 P2216 [HAOI2007]理想的正方形 || 二维RMQ的单调队列

    题目 这个题的算法核心就是求出以i,j为左上角,边长为n的矩阵中最小值和最大值.最小和最大值的求法类似. 单调队列做法: 以最小值为例: q1[i][j]表示第i行上,从j列开始的n列的最小值.$q1 ...

  8. Party Games UVA - 1610

    题目 #include<iostream> #include<string> #include<algorithm> using namespace std; // ...

  9. SUSAN角点检测

    close all; clear all; I=imread('corner2.gif'); [posX,posY]=susan(I,); figure; imshow(I);hold on; plo ...

  10. 当不知道基本数据类型的取值范围时,可以通过max_value等来查询

    public class Demo03{ public static void main(String[] args){ System.out.println("int MAX " ...