【leetcode】atoi (hard) ★
虽然题目中说是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) ★的更多相关文章
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
- 【leetcode】893. Groups of Special-Equivalent Strings
Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...
- 【leetcode】657. Robot Return to Origin
Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...
- 【leetcode】557. Reverse Words in a String III
Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...
随机推荐
- 【PHP面向对象(OOP)编程入门教程】14.final关键字的应用
这个关键字只能用来定义类和定义方法, 不能使用final这个关键字来定义成员属性,因为final是常量的意思,我们在PHP里定义常量使用的是define()函数,所以不能使用final来定义成员属性. ...
- 【C语言入门教程】4.10 综合实例 - 媒体播放器
4.10.1 建立播放列表 数据字典 名称 数据类型 说明 MAX_LENGTH 符号常量 用于定义数组长度,表示列表最大长度 MAX_FILE_LENGTH 符号常量 用于定义数组长度,表示文件名最 ...
- 第五天 loadmore
mutating func loadFresh(completion: (result: APIResult<DeserializedType>) -> ()) -> Canc ...
- mongoDB--1 概念
1.结构 (1)关系型数据库,数据库,对应mongo中的,数据库 (2)关系型数据库,表,对应mongo中的,集合 (3)关系型数据库,一条记录,对应mongo中的,一个文档(是一条json结构的数据 ...
- java之stream(jdk8)
一.stream介绍 参考: Java 8 中的 Streams API 详解 Package java.util.stream Java8初体验(二)Stream语法详解 二.例子 im ...
- LazyLoad.js及scrollLoading.js
http://blog.csdn.net/ning109314/article/details/7042829 目前图片延迟加载主要分两大块,一是触发加载(根据滚动条位置加载图片):二是自动预加载(加 ...
- css小常识
static:对象遵循常规流.此时4个定位偏移属性不会被应用(即 没有声明position:relative, top.right.bottom.left不管用). 当position的值为非stat ...
- 热更新脚本C#light,ulua,Scorpio性能比较
http://www.unity蛮牛.com/thread-32861-1-1.html 测试环境: unity4.5.2 三个脚本全是源码导入 PC :处理器 Intel(R) Core(TM) ...
- 专业版Unity技巧分享:使用定制资源配置文件
http://unity3d.9tech.cn/news/2014/0116/39639.html 通常,在游戏的开发过程中,最终会建立起一些组件,通过某种形式的配置文件接收一些数据.这些可能是程序级 ...
- 自动布局之autoresizingMask使用详解(Storyboard&Code)
自动布局之autoresizingMask使用详解(Storyboard&Code) http://www.cocoachina.com/ios/20141216/10652.html 必须禁 ...