虽然题目中说是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. mock.js-无需等待,让前端独立于后端进行开发

    概述 首先啦,我不认识mock.js的作者,带着需求找到mock.js让我觉得很惊艳. 相对于其他同类的框架的实现,mock.js超出了我的意料. 基于 数据模板 生成模拟数据. 基于 HTML模板 ...

  2. [译]git reset

    git reset 如果说git revert是一个安全的撤销方式, 那么git reset就是一个非常危险的方法了. 当你使用git reset撤销的时候, 你没有可能在回到最初了-他是一个永久的不 ...

  3. Ionic 常见问题及解决方案

    前言 Ionic是目前较为流行的Hybird App解决方案,在Ionic开发过程中会遇到很多常见的开发问题,本文尝试对这些问题给出解决方案. 一些常识与技巧 list 有延迟,可以在ion-cont ...

  4. Ubuntu10.04下安装Ns2的一系列错误及解决方案

    安装之前改一下nam1.11下的agent.h文件73行 Null改为0 第一个错误: xxx configuration: Syntax error: Unterminated quoted str ...

  5. POJ 1265 Area

    有一种定理,叫毕克定理....                             Area Time Limit: 1000MS   Memory Limit: 10000K Total Sub ...

  6. MarkDown 语法

    1. 代码注释 ` ` 2.标题注释 # 3. 文章注释 ```` ```` 4.超链接 This is [an example](http://example.com/ "Title&qu ...

  7. tcl调用tuxedo的一个demo

    Tuxedo是Oracle的一个商业中间件,一般用于通信. 如下是一个简单的调用tuxedo的so动态库实现,编译后,tcl脚本可以加载并调用这个so库实现对tuxedo的调用. 1. 代码例子,保存 ...

  8. FineUI第十一天---布局概述

    布局概述 1.填充整个页面: 让整个容器填充整个页面,设置PageManager的AutoSizePanelID为需要填充整个页面的容器控件ID. 2. 填充整个容器(Fit): 让一个控件填满另一个 ...

  9. HashMap原理分析

    HashMap 实现Map.Cloneable.Serializable接口,继承AbstractMap基类. HashMap map = new HashMap(); 实例化一个HashMap,在构 ...

  10. 观察者(Observer)模式

    http://www.cnblogs.com/zhenyulu/articles/73723.html 一. 观察者(Observer)模式 观察者模式又叫做发布-订阅(Publish/Subscri ...