虽然题目中说是easy, 但是我提交了10遍才过,就算hard吧。

主要是很多情况我都没有考虑到。并且有的时候我的规则和答案中的规则不同。

答案的规则:

1.前导空格全部跳过  “      123”  = 123

2.正负号要考虑   “+123” = 123  “-123” = -123

3.数字的前导0要跳过  “-0000123” = “-123”

4.在数字阶段遇到非数字值,数字截断  “-0000 123” = 0   “123a213" = 123

5.没有有效数字,返回0  ”+-123“ = 0

6.数字越界,返回 最大值2147483647 或最小值 -2147483648

思路:先用strcpy获取有效的数字部分(先跳过前导空格,获取正负号,截至到非数字部分)

再判断strcpy长度,为0或只有一个+-号,返回0.  (没有有效数字)

有有效数字,把获取的数字转换成字符串,strcmp判断是否相同。不同表示数字溢出。

若输入数字是正数,返回最大值,反之返回最小值。

#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <string.h>
using namespace std; class Solution {
public:
int atoi(const char *str)
{
char scheck[]; //判断是否溢出
char strcpy[]; //输入字符串的有效数字部分
int ans = ;
int i = ;
int icpy = ; //去掉前导空格
while(str[i] == ' ')
{
i++;
}
if(str[i] == '+' || str[i] == '-')
{
strcpy[icpy++] = str[i++];
}
//去掉紧跟正负号的前导0
while(str[i] == '')
{
i++;
} for(;str[i] != '\0'; i++)
{
if( '' <= str[i] && str[i] <= '')
{
ans = ans * + (str[i] - '');
strcpy[icpy++] = str[i];
}
else
{
break;
}
}
strcpy[icpy] = '\0'; if(strlen(strcpy) == )
{
return ;
}
else if(strlen(strcpy) == && (strcpy[] == '+' || strcpy[] == '-'))
{
return ;
} if(strcpy[] == '-')
{
ans = - ans;
sprintf(scheck, "%d", ans);
}
else if(strcpy[] == '+')
{
scheck[] = '+';
sprintf(scheck+, "%d", ans);
}
else
{
sprintf(scheck, "%d", ans);
} if(strcmp(scheck, strcpy) != )
{
if(strcpy[] == '-')
{
ans = -;
}
else
{
ans = ;
}
}
return ans;
}
}; int main()
{
Solution s;
char str[] = "";
int ans = s.atoi(str);
return ;
}

【leetcode】atoi (hard) ★的更多相关文章

  1. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  2. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  3. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  4. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  5. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  6. 【刷题】【LeetCode】000-十大经典排序算法

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法

  7. 【leetcode】893. Groups of Special-Equivalent Strings

    Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...

  8. 【leetcode】657. Robot Return to Origin

    Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...

  9. 【leetcode】557. Reverse Words in a String III

    Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...

随机推荐

  1. 预处理prepareStatement是怎么防止sql注入漏洞的?

    序,目前在对数据库进行操作之前,使用prepareStatement预编译,然后再根据通配符进行数据填值,是比较常见的做法,好处是提高执行效率,而且保证排除SQL注入漏洞. 一.prepareStat ...

  2. php加速运行优化

    一个系统的运行性能,除了程序本身要写的完善,还有要看php本身的一些问题,对于php的运行优化,主要有这些加速器:wincache,xcache,ZendOPcache,eAccelerator加速器 ...

  3. VTK初学一,a_Vertex图形点的绘制

    系统:Win8.1 QT版本:2.4.2,Mingw VTK版本:6.3 2. main.cpp #ifndef INITIAL_OPENGL #define INITIAL_OPENGL #incl ...

  4. LYDSY模拟赛day3 序列

    NOIP不考可持久,弃坑

  5. php综合应用

    php面试题之五--PHP综合应用(高级部分) 五.PHP综合应用 1.写出下列服务的用途和默认端口(新浪网技术部) ftp.ssh.http.telnet.https ftp:File Transf ...

  6. js兼容注意事项--仅供参考

    做BS开发就难免会用到javascript,而每个浏览器对javascript的支持有不同.这就需要我们程序员去兼容他们,不然有些浏览器就无法运行我们的代码.就会造来客户的投诉,如果让BoSS知道了, ...

  7. JavaScript的面临的9个陷阱

    虽然不是什么很高深的技术问题,但注意一下,会使您的编程轻松些,即所谓make life easier. 笔者对某些陷阱会混杂一些评点. 1.   最后一个逗号 如这段代码,注意最后一个逗号,按语言学角 ...

  8. HDU 3911 线段树区间合并、异或取反操作

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3911 线段树区间合并的题目,解释一下代码中声明数组的作用: m1是区间内连续1的最长长度,m0是区间内连续 ...

  9. linux 下安装ftp服务器

    最后重启    # service vsftpd restart   1.查看是否安装vsftp rpm -qa | grep ftp 如果出现    vsftpd-2.0.5-16.el5_5.1 ...

  10. Java Io 字符流

    Java Io 字符流包含: 1. InputStreamReader  它是由byte流解析为char流,并且按照给定的编码解析. 2. OutputStreamWrite  它是char流到byt ...