虽然题目中说是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. Windows Server 2008 R2 IIS7.5下PHP、MySQL快速环境配置【图】

    众所周知,win平台的服务器版本默认是不能运行php的,需要对服务器进行环境配置. 而许多朋友纠结如何配置,在百度上搜索出的教程一大堆,基本步骤复杂,新手配置容易出错. 今天,邹颖峥教大家一种快速配置 ...

  2. Tomcat6.0 管理器配置

    最近忙着毕业答辩,填写材料,好多事情都给耽搁了!一个月都没有继续翻译tomcat,这回有点时间赶紧补上. 这部分,其实对开发者或者tomcat管理者来说,只要会登录页面管理器或者使用写简单的http就 ...

  3. 关于QString::toWCharArray 无法解析的外部符号

    1>CommManger.obj : error LNK2019: 无法解析的外部符号 "public: int __thiscall QString::toWCharArray(un ...

  4. POJ 1061 青蛙的约会

                            青蛙的约会 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 82859   A ...

  5. button事件驱动

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  6. CentOS启用sudo,禁用root远程登录

    CentOS默认不启用sudo,且可以直接用超级管理员身份登录服务器.ubuntu这方面做得比较好,为了安全,减小误操作带来的损失,还是推荐启用sudo. 1.添加sudo用户 执行 visudo 命 ...

  7. 给Windows + Apache 2.2 + PHP 5.3 安装PHP性能测试工具 xhprof_0.10.3_php53_vc9.dll

    1.下载XHProf 到这里 http://dev.freshsite.pl/php-extensions/xhprof.html 下载Windows版本的XHProf,我这里选择下载 XHProf ...

  8. iOS开发——UI基础-懒加载,plist文件,字典转模型,自定义view

    一.懒加载 只有使用到了商品数组才会创建数组 保证数组只会被创建一次 只要能够保证数组在使用时才创建, 并且只会创建一次, 那么我们就称之为懒加载 lazy - (void)viewDidLoad 控 ...

  9. 为 Github 创造 Integration

    导读 现在你可以从我们的 集成件目录里面找到更多工具.这个目录目前有超过 15 个分类 — 从 API 管理 到 应用监控, Github 的集成件可以支持您的开发周期的每一个阶段. 我们邀请了具有不 ...

  10. Bootstrap IIFE

    在Bootstrap源码(具体请看<Bootstrap源码解析>)和其他jQuery插件经常看到如下的写法: +function ($) { }(window.jQuery); 这种写法称 ...