(letcode)String to Integer (atoi)
Implement atoi to convert a string to an integer.
Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.
Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.
问题:将字符窜转换成数字
分析:感觉题目不难,但是细节很多,容易想不到
1.数字前面有空格 如s=“ 123456”
2.数字前出现了不必要或多于1个的字符导致数字认证错误,输出0 如s=“ b1234” ,s=“ ++1233” , s=“ +-1121”
3.数字中出现了不必要的字符,返回字符前的数字 如s=“ 12a12” , s=“ 123 123”
4.数字越界 超过了范围(-2147483648--2147483647) 若超过了负数的 输出-2147483648 超过了正数的输出2147483647
在科普一个知识点,倘若某个数超过了2147483647则会变为负数,反过来一样
class Solution {
public:
int myAtoi(string str) {
long long result = ;
int i = ;
int flag1 = ;
int flag2 = ;
if(str.empty()) return ;
while(str[i] != '\0' && str[i] == ' ') i++;
while(str[i] == '-') { flag1++;i++; }
while(str[i] == '+') { flag2++;i++; }
while(i < str.length())
{
if(str[i] >= '' && str[i] <= '')
{
if(flag1 > ||flag2> || (flag1 == &&flag2 == ))
{
result = ;
break;
}
else if(flag1 == )
{
result = result * - (str[i]-'');
if(result < INT_MIN) result = INT_MIN;
}
else
{
result = result * + (str[i]-'');
if(result > INT_MAX) result = INT_MAX;
}
i++;
}
else
{
break;
}
}
int num = (int)result;
return num;
}
};
(letcode)String to Integer (atoi)的更多相关文章
- 【leetcode】String to Integer (atoi)
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- No.008 String to Integer (atoi)
8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...
- leetcode第八题 String to Integer (atoi) (java)
String to Integer (atoi) time=272ms accepted 需考虑各种可能出现的情况 public class Solution { public int atoi( ...
- 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 字符 ...
- Kotlin实现LeetCode算法题之String to Integer (atoi)
题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 class ...
- LeetCode--No.008 String to Integer (atoi)
8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...
- leetcode-algorithms-8 String to Integer (atoi)
leetcode-algorithms-8 String to Integer (atoi) Implement atoi which converts a string to an integer. ...
- LeetCode: String to Integer (atoi) 解题报告
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
随机推荐
- wp ApplicationBar
WP7中的菜单栏 一个应用程序的菜单栏的内容是有限的,最多不超过4个,排列顺序是以菜单栏中间为中心,然后从左往右排列. WP7提供了两种类型的菜单栏,一种是全局的菜单栏,也就是说在所有的页面中都会出现 ...
- TYVJ P1078 删数 Label:区间dp
描述 有N个不同的正整数数x1, x2, ... xN 排成一排,我们可以从左边或右边去掉连续的i个数(只能从两边删除数),1<=i<=n,剩下N-i个数,再把剩下的数按以上操作处理,直到 ...
- Asp.Net 常用代码-备用
1.DropDownList 复制 //DropDownList DropDownList2.Items.Clear(); foreach (ListItem li in DropDownList1. ...
- replication set复制集
replication set复制集 介绍 replicattion set 多台服务器维护相同的数据副本,提高服务器的可用性,总结下来有以下好处: 数据备份与恢复 读写分离 MongoDB 复制集 ...
- explicit关键字
C++中,explicit关键字用来修饰类只有一个参数的构造函数,被修饰的构造函数的类,不能发生相应的隐式类型转换,只能以显示的方式进行类型转换. explicit使用注意事项: explicit 关 ...
- Redis在windows环境下的部署
一.下载 官网地址:http://redis.io/download Git地址:https://github.com/MSOpenTech/redis 注:官方无windows版本,需要window ...
- Frenetic Python实验(三)
实验5 repeater 这个实验在HelloSDNWorld里面做的实验是一样的.HelloSDNWorld 目的:模拟一个有多个端口的中继器. This application implement ...
- 【CEDEC 2015】【夏日课堂】制作事宜技术篇,新手职员挑战VR Demo开发的真相
日文原文地址 http://www.4gamer.net/games/277/G027751/20150829002/ PS:CEDEC 2015的PPT有些要到10月才有下载,目前的都是记者照片修图 ...
- Unity3D之游戏架构脚本该如何来写(转)
这篇文章主要想大家说明一下我在Unity3D游戏开发中是如何写游戏脚本的,对于Unity3D这套游戏引擎来说入门极快,可是要想做好却非常的难.这篇文章的目的是让哪些已经上手Unity3D游戏引擎的朋友 ...
- empty($w)
<?php $w = ''; var_dump(empty($w)); $w = ' '; var_dump(empty($w)); $w = 0; var_dump(empty($w)); v ...