【Leetcode】【Easy】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.
写一个a to i 字符串转为整形的函数。
注意考虑所有可能的输入情况。
题目不设置具体的输入规范,你需要思考收集所有可能的输入。
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.
函数要求:
函数首先应该丢弃字符串首字符前的空白格;
判断数字的正负符号,并将后面跟着的数字符号转化为数值;
在数字字符之后,可能还包含其他不相关字符,这些字符应该省去;
如果在空白格后,第一个字符不是有效的数字字符,或者根本没有有效的数字字符存在,那么不需要进行转化,返回空;
如果没有有效的字符可以进行转化,那么返回0;
如果正确的数值超过了能表示的范围,那么返回INT_MAX或者INT_MIN。
解题:
对于一个输入,基本需要考虑4种情况:
1、删去头部的空白格(空格);
2、判断数字的正负;
3、注意整形溢出;
4、处理无效的输入。
解题步骤:
1、不管输入是char *还是 stl string,需要三个int变量,正负号sign,结果值base,操作序号i;
2、判断输入是否有效;
3、删去头部的空格;
4、判断正负符号,用一些技巧使正时sign为1,负时sign为0;
5、判断字符是否是数字,如果是:
(1)在计算此数字之前,查看当前base是否已经越界,即是否能安全的计算当前数字;
即 base < INT_MAX / 10; 或者 base == INT_MAX / 10 && 当前数字 <= INT_MAX % 10;
(2)计算当前数字;
6、返回代符号的base;
代码借鉴自Leetcode上题后讨论
class Solution {
public:
int atoi(const char *str) {
int sign = , base = , i = ; while (str[i] == ' ')
i++; if (str[i] == '-' || str[i] == '+') {
sign = - * (str[i++] == '-');
} while (str[i] >= '' && str[i] <= '') {
if (base > INT_MAX / || (base == INT_MAX / && str[i] - '' > )) {
if (sign == )
return INT_MAX;
else
return INT_MIN;
}
base = * base + (str[i++] - '');
} return base * sign;
}
};
【Leetcode】【Easy】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 ...
- LeetCode【8】. String to Integer (atoi) --java实现
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【LeetCode题意分析&解答】35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- 【Leet Code】String to Integer (atoi) ——常考类型题
String to Integer (atoi) Total Accepted: 15482 Total Submissions: 106043My Submissions Implement ato ...
- 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 ...
- Kotlin实现LeetCode算法题之String to Integer (atoi)
题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 class ...
- LeetCode: String to Integer (atoi) 解题报告
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)
Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...
随机推荐
- 未在本地计算机上注册microsoft.jet
未在本地计算机上注册microsoft.jet http://www.microsoft.com/zh-cn/download/confirmation.aspx?id=13255
- Codeforces Round #462 (Div. 2), problem: (C) A Twisty Movement (求可以转一次区间的不递增子序列元素只有1,2)
题目意思: 给长度为n(n<=2000)的数字串,数字只能为1或者2,可以将其中一段区间[l,r]翻转,求翻转后的最长非递减子序列长度. 题解:求出1的前缀和,2的后缀和,以及区间[i,j]的最 ...
- UVA - 10817 状压DP
题意:大白P95 本题比较特别的是状压两个集合并且进行转移,因此要分别处理当前集合只有1个老师/2个老师的记录(然后可O(1)得出0个老师的集合) 记忆化过了但是迭代式不能记忆超过2的之前的状态是怎样 ...
- java 上传文件到 ftp 服务器
1. java 上传文件到 ftp 服务器 package com.taotao.common.utils; import java.io.File; import java.io.FileInpu ...
- JedisPool
package redis; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis. ...
- Phyton Flask框架学习记录。
注意:在左侧菜单栏(在JQuery插件库下载的)右边是采用<iframe> 标签嵌入其他页面,此时标签的src应用用后台中的方法名称(本人测试用的是无参数的方法), 而页面跳转window ...
- (转)同步异步,阻塞非阻塞 和nginx的IO模型
同步异步,阻塞非阻塞 和nginx的IO模型 原文:https://www.cnblogs.com/wxl-dede/p/5134636.html 同步与异步 同步和异步关注的是消息通信机制 (sy ...
- 牛客网Java刷题知识点之OSI七层参考模型 和 TCP/IP五层参考模型
不多说,直接上干货! 福利 => 每天都推送 欢迎大家,关注微信扫码并加入我的4个微信公众号: 大数据躺过的坑 Java从入门到架构师 人工智能躺过的坑 ...
- opensuse12.3 桌面设置备忘录
工作空间外观 窗口装饰 ghost deco2.2 光标主题 ringG 桌面主题 ghost 欢迎屏幕 login-scan-splash-cg 应用程序外观 风格 部件样式 Oxygen 颜色 g ...
- pat04-树4. Root of AVL Tree (25)
04-树4. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue An A ...