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 ...
随机推荐
- Django的学习进阶(三)————ORM
django框架是将数据库信息进行了封装,采取了 类——>数据表 对象——>记录 属性——>字段 通过这种一一对应方式完成了orm的基本映射官方文档:https://docs.dja ...
- 盘一盘 synchronized (二)—— 偏向锁批量重偏向与批量撤销
在本文讲解之前,先来简单了解一下为什么会有批量重偏向和批量撤销. 批量重偏向:当一个线程创建了大量对象并执行了初始的同步操作,后来另一个线程也来将这些对象作为锁对象进行操作,会导偏向锁重偏向的操作 ...
- python3 读取文件-2
1.脚本 from sys import argv script,filename = argv#以读的模式打开txt文件txt = open(filename,'r+')print ("t ...
- 对ThreadLocal的一些理解
ThreadLocal也是在面试过程中经常被问到的,本文主要从以下三个方面来谈对ThreadLocal的一些理解: ThreadLocal用在什么地方 ThreadLocal一些细节 ThreadLo ...
- 真千兆路由的极限之OPENWRT MAKE, 某品牌白菜价QCA9558/QCA9880/QCA8337N纯种组合OS搭建时记
自从上次仙人梦里放了一张无字天书,解惑了WPR003N的秘诀后,渐渐的,就忘了这件这事情,连想好的评测都拖延了好多月了,毕竟路由是拿来用的,不是用来写什么陈词滥调的评测的,无意间,热爱白菜的我发现了一 ...
- WPF界面的异步后台加载
private void Init() { BackgroundWorker worker = new BackgroundWorker(); ...
- 第三章 Linux基本命令操作
第三章 Linux基本命令操作 ¨ 本节所讲内容: ¨ 3.1 Linux终端介绍 Shell提示符 Bash Shell基本语法 ¨ 3.2 基本命令的使用:ls.pwd.cd.hist ...
- MyBatis 一级缓存、二级缓存全详解(一)
目录 MyBatis 一级缓存.二级缓存全详解(一) 什么是缓存 什么是MyBatis中的缓存 MyBatis 中的一级缓存 初探一级缓存 探究一级缓存是如何失效的 一级缓存原理探究 还有其他要补充的 ...
- Hadoop学习(9)-spark的安装与简单使用
spark和mapreduce差不多,都是一种计算引擎,spark相对于MapReduce来说,他的区别是,MapReduce会把计算结果放 在磁盘,spark把计算结果既放在磁盘中有放在内存中,ma ...
- django drf框架中的user验证以及JWT拓展的介绍
登录注册是几乎所有网站都需要去做的接口,而说到登录,自然也就涉及到验证以及用户登录状态保存,最近用DRF在做的一个关于网上商城的项目中,引入了一个拓展DRF JWT,专门用于做验证和用户状态保存.这个 ...