8. String to Integer (atoi)
题目:
Implement atoi to convert a string to an integer.
Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.
Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.
spoilers alert... click to show requirements for atoi.
Requirements for atoi: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. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.
解析:注意前后中间(或全是)的空格,注意正负号,注意溢出,(题目要求中没有:注意特殊字符出现)
代码也许还待优化:
//#define INT_MAX 2147483647
//#define INT_MIN -2147483648 double noHeadBlank(const char *str){
double val = 0;
if(str[0] == '-' || str[0] == '+'){
int i = 1;
while(str[i] == '0') ++i;
if(str[i] == '\0') return 0;
else if(str[i] > '0' && str[i] <= '9') {val = str[i] - '0'; ++i;}
else return 0;
for(; str[i] != '\0' && str[i] >= '0' && str[i] <= '9'; ++i)
val = val * 10 + (str[i] - '0');
if(str[0] == '-') val *= (-1);
}else if(str[0] > '0' && str[0] <= '9') {
val = str[0] - '0';
int i = 1;
for(; str[i] != '\0' && str[i] >= '0' && str[i] <= '9'; ++i){
val = val * 10 + (str[i] - '0');
}
}else if(str[0] == '0'){
int i = 1;
while(str[i] == '0') ++i;
if(str[i] == '\0') return 0;
else if(str[i] > '0' && str[i] <= '9') {val = str[i] - '0'; ++i;}
else return 0;
for(; str[i] != '\0' && str[i] >= '0' && str[i] <= '9'; ++i)
val = val * 10 + (str[i] - '0');
}
return val;
} class Solution {
public:
int atoi(const char *str) {
double val = 0;
if(str == NULL) return 0;
if(str[0] == ' '){
int i = 1;
while(str[i] == ' ') ++i;
if(str[i] == '\0') return 0;
else val = noHeadBlank(str+i);
}else val = noHeadBlank(str);
if(val > (double)INT_MAX) return INT_MAX;
else if (val < (double)INT_MIN) return INT_MIN;
else return (int)val;
}
};
8. String to Integer (atoi)的更多相关文章
- 【leetcode】String to Integer (atoi)
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- No.008 String to Integer (atoi)
8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...
- leetcode第八题 String to Integer (atoi) (java)
String to Integer (atoi) time=272ms accepted 需考虑各种可能出现的情况 public class Solution { public int atoi( ...
- leetcode day6 -- String to Integer (atoi) && 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 ...
- String to Integer (atoi) - 字符串转为整形,atoi 函数(Java )
String to Integer (atoi) Implement atoi to convert a string to an integer. [函数说明]atoi() 函数会扫描 str 字符 ...
- Kotlin实现LeetCode算法题之String to Integer (atoi)
题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 class ...
- LeetCode--No.008 String to Integer (atoi)
8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...
- leetcode-algorithms-8 String to Integer (atoi)
leetcode-algorithms-8 String to Integer (atoi) Implement atoi which converts a string to an integer. ...
- LeetCode: String to Integer (atoi) 解题报告
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- 《LeetBook》leetcode题解(8): String to Integer (atoi) [E]——正负号处理
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
随机推荐
- css布局之一列布局
在我们浏览网页中经常看见一列布局其实一列布局就是 一般的一列布局的都是固定宽度的 body{margin:0;padding:0} .main{width:800px;height:300px;bac ...
- android贪吃蛇(超级简陋版)
public class body { public int ax;//代表X周变量 public int ay;//代表Y轴变量 public int getAx() { return ax; } ...
- 搭建Android工程的步骤及其第一个安卓程序
1.安卓系统架构 1>底层是Linux系统 2>函数库层 由C或C++写的 3>Application frameWork应用的框架层 4>顶层是应用层 2.JVM与DVM介绍 ...
- Oracle数据库习题
以下习题都已Oracle数据库中默认表为主体 1.列出至少有一个员工的所有部门. SELECT DISTINCT D.DNAME FROM EMP E,DEPT D WHERE E.DEPTNO=D. ...
- 数据结构C语言实现系列——线性表(线性表链接存储(单链表))
#include <stdio.h>#include <stdlib.h>#define NN 12#define MM 20typedef int elemType ;/** ...
- viewpager接受值图片轮播
package com.baway.test; import java.util.ArrayList;import java.util.List;import java.util.Timer;impo ...
- Web Storage的方法
1.分为两种:localStorage与sessionStorage.2.存储形式:key-value的形式.sessionStorage 1.session定义:session指用户在浏览某个网站时 ...
- POJ 3321 Apple Tree(树状数组)
Apple Tree Time Limit: 2000MS Memory Lim ...
- 【转】Reactor与Proactor两种模式区别
转自:http://www.cnblogs.com/cbscan/articles/2107494.html 两种IO多路复用方案:Reactor and Proactor 一般情况下,I/O 复用机 ...
- yii2 使用composer安装
composer global require "fxp/composer-asset-plugin:~1.0.0" composer create-project --prefe ...