leetcode8 String to Integer
题目描述
Implement atoi which converts a string to an integer.
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.
Note:
Only the space character ' ' is considered as whitespace character.
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned.
Example 1:
Input: "42"
Output: 42
Example 2:
Input: " -42"
Output: -42
Explanation: The first non-whitespace character is '-', which is the minus sign.
Then take as many numerical digits as possible, which gets 42.
Example 3:
Input: "4193 with words"
Output: 4193
Explanation: Conversion stops at digit '3' as the next character is not a numerical digit.
Example 4:
Input: "words and 987"
Output: 0
Explanation: The first non-whitespace character is 'w', which is not a numerical
digit or a +/- sign. Therefore no valid conversion could be performed.
Example 5:
Input: "-91283472332"
Output: -2147483648
Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer.
Thefore INT_MIN (−231) is returned.
我的解法
class Solution {
public:
int myAtoi(string str) {
int notfirst=0;
int fuhao=1;
long long int result=0;
for(char s:str){
if(s==' '&¬first==1)
break;
if(s==' ')
continue;
if(notfirst&&(s<'0'||s>'9'))
break;
if(s=='-')
{
fuhao=-1;
notfirst=1;
continue;
}
if(s=='+')
{
fuhao=1;
notfirst=1;
continue;
}
if(s<'0'||s>'9')
break;
else if(s>='0'&&s<='9')
{
notfirst=1;
result=result*10+s-'0';
if(result>INT_MAX && fuhao==1)
return INT_MAX;
else if(result>INT_MAX && fuhao==-1)
return INT_MIN;
else if(fuhao ==1 && result <INT_MIN){
return INT_MIN;
}else if(fuhao==-1 && result< INT_MIN){
return INT_MAX;
}
}
}
result=result * fuhao;
return result;
}
};
总结
我发现思路很容易出来,但是编写的时候总是会漏掉各种情况。这种有点像csp的第三题大模拟,不过需要自己想特殊情况的处理。
leetcode8 String to Integer的更多相关文章
- LeetCode----8. String to Integer (atoi)(Java)
package myAtoi8; /* * Implement atoi to convert a string to an integer. Hint: Carefully consider all ...
- leetcode8 String to Integer (atoi)
题目需求: 输入一个字符串,输出对应的int值 特殊处理: 输入: null 输出:0 输入: "a122" 输出:0 输入: " 1233" 输出: ...
- Leetcode8.String to Integer (atoi)字符串转整数(atoi)
实现 atoi,将字符串转为整数. 该函数首先根据需要丢弃任意多的空格字符,直到找到第一个非空格字符为止.如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连续的数字组合起来,这部分字 ...
- LeetCode 8. 字符串转换整数 (atoi)(String to Integer (atoi))
8. 字符串转换整数 (atoi) 8. String to Integer (atoi) 题目描述 LeetCode LeetCode8. String to Integer (atoi)中等 Ja ...
- 【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】7 & 8 - Reverse Integer & String to Integer (atoi)
7 - Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Notic ...
- 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 ...
随机推荐
- vue.js带复选框表单的增删改查
近段时间由于公司项目要求,前端开始使用VUE框架进行开发,最近刚开始学习,做了一个表单的增删改查,和大家分享一下. 页面模型代码设计如下 <template> <div id=&qu ...
- 基于redis的消息订阅与发布
Redis 的 SUBSCRIBE 命令可以让客户端订阅任意数量的频道, 每当有新信息发送到被订阅的频道时, 信息就会被发送给所有订阅指定频道的客户端. 作为例子, 下图展示了频道 channel1 ...
- idea中写servlet时报错--关于405错误
将super方法注释掉 原因:super是调用了此类继承父类doget和dopost方法的, 如果此类中没有这个方法,就会报错The specified HTTP method is not allo ...
- Python实现淘宝秒杀聚划算自动提醒源码
快来加入群[python爬虫交流群](群号570070796),发现精彩内容. 本实例能够监控聚划算的抢购按钮,在聚划算整点聚的时间到达时发出提醒(音频文件自己定义位置)并自动弹开页面(URL自己定义 ...
- xpath定位的一些方法
- redis实现排行榜
1 前言 实现一个排版榜,我们通常想到的就是mysql的order by 简单粗暴就撸出来了.但是这样真的优雅吗? 数据库是系统的瓶颈,这是众所周知的.如果给你一张百万的表,让你排序做排行榜,花费的时 ...
- jmeter使用JDBC连接数据库
jmeter使用JDBC的配置元件连接数据库,通过sql语句查询需用到的数据 配置元件名称:JDBC connection configuration,使用前,需导入mysql-connector-j ...
- Go中的指针
学Java以来,让程序员忽略了指针和内存地址这些概念,Java帮我们封装了对象,简化了对象引用之间的关系.在Go语言中,又帮我们回忆起这些概念. 我们创建的每一个对象在内存中都有一个位置去存储,每个内 ...
- 2019牛客多校训练第三场H.Magic Line(思维)
题目传送门 大致题意: 输入测试用例个数T,输入点的个数n(n为偶数),再分别输入n个不同的点的坐标,要求输出四个整数x1,y1,x2,y2,表示有一条经过点(x1,y1),(x2,y2)的直线将该二 ...
- CSS3:pointer-events | a标签禁用
用纯css就能实现取消事件响应的方法,pointer-events,使用起来更加简单,它可以: pointer-events: auto | none | visiblePainted | visib ...