题目描述

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==' '&&notfirst==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的更多相关文章

  1. LeetCode----8. String to Integer (atoi)(Java)

    package myAtoi8; /* * Implement atoi to convert a string to an integer. Hint: Carefully consider all ...

  2. leetcode8 String to Integer (atoi)

    题目需求: 输入一个字符串,输出对应的int值 特殊处理: 输入: null  输出:0 输入: "a122"  输出:0 输入: "   1233"  输出: ...

  3. Leetcode8.String to Integer (atoi)字符串转整数(atoi)

    实现 atoi,将字符串转为整数. 该函数首先根据需要丢弃任意多的空格字符,直到找到第一个非空格字符为止.如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连续的数字组合起来,这部分字 ...

  4. LeetCode 8. 字符串转换整数 (atoi)(String to Integer (atoi))

    8. 字符串转换整数 (atoi) 8. String to Integer (atoi) 题目描述 LeetCode LeetCode8. String to Integer (atoi)中等 Ja ...

  5. 【leetcode】String to Integer (atoi)

    String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...

  6. No.008 String to Integer (atoi)

    8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...

  7. 【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 ...

  8. leetcode第八题 String to Integer (atoi) (java)

    String to Integer (atoi) time=272ms   accepted 需考虑各种可能出现的情况 public class Solution { public int atoi( ...

  9. leetcode day6 -- String to Integer (atoi) &amp;&amp; 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 ...

随机推荐

  1. Office2010安装问题锦集

    问题一,每次打开office 2010,都会出现重新配置的对话框. 解决姿势: 大部分搜索到的解决方法大概有2种, 一个是修改注册表,在注册表中这个这个位置增加一个名为NoRereg的32位World ...

  2. 熟悉软件的生命周期AND测试工程师的工作流程

    1.软件的生命周期 *软件生命周期(SDLC)是软件开始研制到最终被废弃不用所经历的各个阶段.在不同阶段里,由不同的组织.个人和资源进行着明确的任务. 2.生命周期的模型 *常见的生命周期模型有:瀑布 ...

  3. mybatis动态插入数据库

    <insert id="dynamicAddUser"> insert into t_user <!-- trim 对所有的表中列名 进行动态处理 --> ...

  4. .Net Core CLR FileFormat Call Method( Include MetaData, Stream, #~)

    .Net Core  CLR PE 文件启动方法,找到函数入口点,调用整个.Net 程式宿主. 使用方法:可以利用Visual Studio新建一个控制台应用程序,然后生成DLL,替换掉本程序DLL, ...

  5. 蓝桥杯 2n皇后问题 深搜

    默认大家会了n皇后问题 基础练习 2n皇后问题   时间限制:1.0s   内存限制:512.0MB     问题描述 给定一个n*n的棋盘,棋盘中有一些位置不能放皇后.现在要向棋盘中放入n个黑皇后和 ...

  6. Unity经典游戏教程之:雪人兄弟

    版权声明: 本文原创发布于博客园"优梦创客"的博客空间(网址:http://www.cnblogs.com/raymondking123/)以及微信公众号"优梦创客&qu ...

  7. JAVA jobs

    Java岗位1, SpringMVC, spring, mybaits2, 高并发编程3, mysql或者oracle SQL调优及函数,存储过程,JOB调度

  8. react学习(二)--元素渲染

    元素用来描述你在屏幕上看到的内容: const element = <h1>Hello, world</h1>; 与浏览器的 DOM 元素不同,React 当中的元素事实上是普 ...

  9. Mysql 分页order by一个相同字段,发现顺序错乱

    两次分页查询,其中跳过了2个id   select * from jdp_tb_trade  where jdp_modified>='2017-04-24 20:22:01' and jdp_ ...

  10. SVN服务器更改ip地址客户端怎么设置

    SVN 服务器 IP 地址修改后,客户端对服务器的连接可以采用以下的方法重定位: 1. 如果客户端工具是TortoiseSVN,直接在工作副本上右键,选择TortoiseSVN->relocat ...