题目描述

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. linux初学者-系统日志(二)

    linux初学者-系统日志(二) 先前在(一)中介绍到在不同主机间日志同步的方法,在一台主机上可以看到另一台主机的日志.这里会介绍系统日志方面的一些其他内容. 1.日志的采集格式 在日志的采集中,由图 ...

  2. Jenkins-slave实现并行的自动化测试

    前言 上篇文章搭建了Jenkins-slave的分布式测试环境,我一直在想一个问题,使用这种模式能不能实现并发的自动化测试?我的想法是:同一套UI自动化的测试代码,是否能够通过一个Job绑定多个sla ...

  3. Linux文件系统损坏导致无法正常启动与fsck修复工具

    今天在打开自己的虚拟机学习的时候,发现在文件系统检查过程中出现以下的报错: /dev/mapper/VolGroup-lv_root:UNEXPECTED INCONSISTENCY;RUN fsck ...

  4. Linux学习笔记06之DNS

    一.DNS概念:Domain Name System(域名系统) 是互联网上作为域名和IP地址相互映射的一个分布式数据库 二.DNS功能: 完成IP地址和域名之间的一个映射 三.DNS分类: 1.静态 ...

  5. ubuntu kylin的桌面崩溃问题

    前几天安了ubuntu kylin,主题还是挺好看的,汉化也很好,就是各种报桌面错误,忍了,结果今天直接进不去桌面了 开机,输入密码,登录,然后桌面死活不显示,还弹出了错误提示我系统有问题,建议重启 ...

  6. [Hei.Captcha] Asp.Net Core 跨平台验证码实现

    写在前面 说起来比较丢脸.我们有个手机的验证码发送逻辑需要使用验证码,这块本来项目里面就有验证码绘制逻辑,.Net Framework的,使用的包是System.Drawing,我把这验证码绘制逻辑复 ...

  7. 去掉matlab图像显示刻度

    图像显示后面加 set( gca, 'XTick', [], 'YTick', [] );

  8. java volatile关键字作用及使用场景

    1. volatile关键字的作用:保证了变量的可见性(visibility).被volatile关键字修饰的变量,如果值发生了变更,其他线程立马可见,避免出现脏读的现象.如以下代码片段,isShut ...

  9. powermockito单元测试之深入实践

    概述 由于最近工作需要, 在项目中要做单元测试, 以达到指定的测试用例覆盖率指标.项目中我们引入的powermockito来编写测试用例, JaCoCo来监控单元测试覆盖率.关于框架的选择, 网上讨论 ...

  10. 什么是redis的缓存雪崩与缓存穿透

    今天来分享一下Redis几道常见的面试题: 如何解决缓存雪崩? 如何解决缓存穿透? 如何保证缓存与数据库双写时一致的问题? 一.缓存雪崩 1.1 什么是缓存雪崩? 首先我们先来回答一下我们为什么要用缓 ...