atoi (String to Integer) leetcode
将字符串转化为数字,其注意事项有:
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.
#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的更多相关文章
- ElasticSearch 5学习(9)——映射和分析(string类型废弃)
在ElasticSearch中,存入文档的内容类似于传统数据每个字段一样,都会有一个指定的属性,为了能够把日期字段处理成日期,把数字字段处理成数字,把字符串字段处理成字符串值,Elasticsearc ...
- Java基础:整型数组(int[]、Integer[])排序
Windows 10家庭中文版,java version "1.8.0_152",Eclipse Oxygen.1a Release (4.7.1a), 参考链接:http://w ...
- 自动拆装箱(int,Integer)
包装类型Java语言是一个面向对象的语言,但是Java中的基本数据类型却是不面向对象的,这在实际使用时存在很多的不便,为了解决这个不足,在设计类时为每个基本数据类型设计了一个对应的类进行代表,这样八个 ...
- Find the duplicate Number (鸽巢原理) leetcode java
问题描述: Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive ...
- 浅谈 Java 字符串(String, StringBuffer, StringBuilder)
我们先要记住三者的特征: String 字符串常量 StringBuffer 字符串变量(线程安全) StringBuilder 字符串变量(非线程安全) 一.定义 查看 API 会发现,String ...
- 02_Redis数据类型(String、Hash)
[Redis数据类型] redis是通过key-Value来存储的,其支持的数据类型如下: 1.字符串 2.Hash 3.List 4.Set 5.SortSet(zset) 注:redis中,命令( ...
- java字符串(String和StringBuilder)
1.String 1.1.创建String对象的方法(三种方式) String s1 = "zhang"; 创建一个字符串对象zhang,名为s1 String s2 = new ...
- LeetCode 8. String to Integer (atoi) (字符串到整数)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- C#LeetCode刷题之#13-罗马数字转整数(Roman to Integer)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3842 访问. 罗马数字包含以下七种字符: I, V, X, L, ...
随机推荐
- caffe 入门实例3 fine-turning
占坑,使用fine-turning初始化参数...
- 3、HTML的body内标签1
一.特殊符号的表示 #代指空格 < #代指,< > #代指,> ...... #这玩意有很多,记也记不完,用的时候查一下即可: 二.p和br标签 <p>< ...
- poj1651【区间DP·基础】
题意: 给你一串数字,头尾不能动,每次取出一个数字,这个数字贡献=该数字与左右相邻数字的乘积,求一个最小值. 思路: 用dp[s][t]去代表s到t的最小值,包括a[s]和a[t],然后从区间为3开始 ...
- DFS系列 POJ(自认为的讲解)
C - Sum It Up POJ1564 题意: 给你一个N,然后给你一堆数The numbers in each list appear in nonincreasing order, and t ...
- IT兄弟连 ElasticSearch在Linux下的安装和启动、常见问题解决
环境要求 · Linux(Centos 7) · ElasticSerach 6.6.1 ES下载 · 下载地址:https://www.elastic.co/cn/do ...
- c++计算程序时间
#include<iostream.h> #include<time.h> void main() { clock_t start,finish; double totalti ...
- Fiddler 学习
Little Skill Fiddler 官网: https://www.telerik.com/fiddler 来自 https://www.cnblogs.com/zhaoyanjun/p/706 ...
- mycat启动报错UnknownHostException(Temporary failure in name resolution)解决方法
重启命令 ./mycat restart 查看日志 cd logs tail -f wrapper.log 报错信息 INFO | jvm 2 | 2018/05/09 11:28:28 | Erro ...
- 关于使用IQKeyBoardManager键盘还是被遮挡的问题解决方案
今天在做一个登录界面的时候发现使用了IQKeyBoardManager键盘还是被遮挡,解决方案如下 解决方案一:在所有视图的最外层添加一个UIView作为容器即可,但在有导航栏的情况下导航栏会跟着向上 ...
- CDH版本大数据集群下搭建的Hue详细启动步骤(图文详解)
关于安装请见 CDH版本大数据集群下搭建Hue(hadoop-2.6.0-cdh5.5.4.gz + hue-3.9.0-cdh5.5.4.tar.gz)(博主推荐) Hue的启动 也就是说,你Hue ...