[LeetCode 题解]: String to Interger (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.
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.
提示: 使用string的特性来判断是否越界。 strcmp
1.去掉字符串之前多余的空格
2. 判断正负号
3. 记录数字字符串。
4. 判断是否越界。
利用好:strcmp
class Solution {
public:
int atoi(const char *str) {
const char* Max = "";
const char* Min = "";
char b[];
int number=;
int below=,i=,j=;
while(str[i]==' ')i++;
if(i==strlen(str)) return ; if(str[i]=='+'||str[i]=='-')
{
if(str[i]=='-') below=;
i++;
} for(;i<strlen(str);i++)
{
if(str[i]>='' && str[i]<='')
b[j++]=str[i];
else
break;
}
b[j]='\0';
int lb = strlen(b);
if(lb>)
{
if(below) return INT_MIN;
else return INT_MAX;
}
else if(lb==)
{
if(below)
{
if(strcmp(b,Min)>=) return INT_MIN;
else
{
for(j=;j<lb;j++)
number= number* + b[j]-'';
}
}
else
{
if(strcmp(b,Max)>=) return INT_MAX;
else
{
for(j=;j<lb;j++)
number= number* + b[j]-'';
}
}
}
else
{
for(j=;j<lb;j++)
number= number* + b[j]-'';
}
return below>?-number:number;
} };
分支结构有点多,需要仔细分析分析。
转载请注明出处: http://www.cnblogs.com/double-win/谢谢
[LeetCode 题解]: String to Interger (atoi)的更多相关文章
- LeetCode题解——String to Integer(atoi)
题目: 字符串转换为数字. 解法: 这道题的意思是要考虑到,如果有前置的空字符,则跳过:如果超出数字范围,则返回最大/最小整数:如果碰到第一个不能转换的字符,则返回. 代码: class Soluti ...
- Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)
Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...
- [LeetCode 题解]: Roman to Interger
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given a ro ...
- [LeetCode][Python]String to Integer (atoi)
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/string- ...
- 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 ...
- 【leetcode】String to Integer (atoi)
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- [leetcode] 8. String to Integer (atoi) (Medium)
实现字符串转整形数字 遵循几个规则: 1. 函数首先丢弃尽可能多的空格字符,直到找到第一个非空格字符. 2. 此时取初始加号或减号. 3. 后面跟着尽可能多的数字,并将它们解释为一个数值. 4. 字符 ...
- Leetcode 8. String to Integer (atoi)(模拟题,水)
8. String to Integer (atoi) Medium Implement atoi which converts a string to an integer. The functio ...
- LeetCode——8. String to Integer (atoi)
一.题目链接:https://leetcode.com/problems/string-to-integer-atoi/ 二.题目大意: 实现一个和C语言里atoi具有相同功能的函数,即能够把字符串转 ...
随机推荐
- 18_java之集合Collection
01集合使用的回顾 *A:集合使用的回顾 *a.ArrayList集合存储5个int类型元素 public static void main(String[] args) { ArrayList< ...
- Linux配置Oracle 11g自动启动
http://www.cnblogs.com/edwardcmh/archive/2012/05/11/2495671.html 安装完毕Oracle 11g每次都得手动启动 | 停止数据库(dbst ...
- python查找文件相同的和包含汉字的
#!/usr/bin/env python # Version = 3.5.2 import os import time d_path = '/data/media' log_file = 'res ...
- BLE 周边设备断开
运行中,突然远程设备断开. TBluetoothLEDevice.IsConnected 为false了. 再次搜索,订阅, BluetoothLE1.SubscribeToCharacteristi ...
- spring cloud zuul 配置(Robbin 和 熔断)
#启动负载均衡的重试机制,默认falsespring.cloud.loadbalancer.retry.enabled=true#Hystrix是否启用超时时间hystrix.command.defa ...
- AngularJS与服务器交互(4)
转自:http://itindex.net/detail/50919-angularjs-%E6%9C%8D%E5%8A%A1%E5%99%A8-%E4%BA%A4%E4%BA%92 对于AJAX应用 ...
- java web 读取配置文件两种方法
package com.tsinghua.getDataBaseConn; import java.io.IOException;import java.io.InputStream;import j ...
- 字符串匹配——C++使用Regex
需要#include < regex > 匹配 regex_match ("subject", std::regex("(sub)(.*)") ...
- 平衡二叉树之AVL树
AVL树(命名来源于作者姓名,Adelson-Velskii和Landis),即平衡二叉树,满足以下的条件: 1)它的左子树和右子树都是AVL树 2)左子树和右子树的高度差不能超过1 从条件1可能看出 ...
- H5 继承
继承 CSS的某些样式是具有继承性的,那么什么是继承呢?继承是一种规则,它允许样式不仅应用于某个特定html标签元素,而且应用于其后代.比如下面代码:如某种颜色应用于p标签,这个颜色设置不仅应用p标签 ...