https://oj.leetcode.com/problems/string-to-integer-atoi/

细节题,把一个字符串转换成整数

class Solution {
public:
int atoi(const char *str) {
if(str == NULL)
return ; // remove all spaces
int i = ;
while(str[i] != '/0' && str[i] == ' ')
{
i++;
}
// only contain spaces
if(str[i] == '/0')
return ; // 判断符号
bool isNegative = false;
if(str[i] == '-')
{
i++;
isNegative = true;
}
else if(str[i] == '+')
i++;
else
{
// not num
if(str[i] < '' || str[i] > '')
return ;
}
double ans = ;
while(str[i] != '/0' && str[i]>='' && str[i] <='')
{
ans = ans*;
ans += str[i] - '';
// 如果是正数
if(isNegative == false && ans >= INT_MAX)
{
ans = INT_MAX;
break;
}
else if(isNegative && ans >= )
{
ans = INT_MIN*(-);
break;
}
i++;
} if(isNegative)
ans = ans *(-); return ans;
}
};

LeetCode OJ-- String to Integer (atoi) **的更多相关文章

  1. LeetCode OJ String to Integer (atoi) 字符串转数字

    #include <iostream> #include <assert.h> using namespace std; int ato(const char *str) { ...

  2. Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)

    Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...

  3. leetcode day6 -- String to Integer (atoi) &amp;&amp; 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 ...

  4. 【leetcode】String to Integer (atoi)

    String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...

  5. [leetcode] 8. String to Integer (atoi) (Medium)

    实现字符串转整形数字 遵循几个规则: 1. 函数首先丢弃尽可能多的空格字符,直到找到第一个非空格字符. 2. 此时取初始加号或减号. 3. 后面跟着尽可能多的数字,并将它们解释为一个数值. 4. 字符 ...

  6. Leetcode 8. String to Integer (atoi)(模拟题,水)

    8. String to Integer (atoi) Medium Implement atoi which converts a string to an integer. The functio ...

  7. [LeetCode][Python]String to Integer (atoi)

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/string- ...

  8. 【LeetCode】String to Integer (atoi) 解题报告

    这道题在LeetCode OJ上难道属于Easy.可是通过率却比較低,究其原因是须要考虑的情况比較低,非常少有人一遍过吧. [题目] Implement atoi to convert a strin ...

  9. LeetCode 8. String to Integer (atoi) (字符串到整数)

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  10. [LeetCode] 8. String to Integer (atoi) 字符串转为整数

    Implement atoi which converts a string to an integer. The function first discards as many whitespace ...

随机推荐

  1. 使用chrome联调不在同一个域的请求

    做前端的,用Ajax获取数据,是常有的事情,同域下自然没问题了,如果是不同域获取数据,浏览器就有个同源策略的限制. Origin * is not allowed by Access-Control- ...

  2. ORACLE 10G 闪回建议

    Flashback Tips The following tips and restrictions apply to using flashback features. Flashback Tips ...

  3. EL简介

    一.EL简介 1.语法结构    ${expression}2.[]与.运算符    EL 提供.和[]两种运算符来存取数据.    当要存取的属性名称中包含一些特殊字符,如.或?等并非字母或数字的符 ...

  4. PHP的命名空间

    简介: 防止名称冲突. 原理: 类似文件目录/usr/local这样的. 用法: namespace:定义命名空间: use:取别名: 代码示例:file 1.php <?php namespa ...

  5. 百度地图API示例之设置级别setZoom与禁止拖拽disableDragging

    百度地图API示例之设置级别setZoom与禁止拖拽disableDragging 设置级别 <html> <head> <meta http-equiv="C ...

  6. shell字符串判空

    主要用到两个命令 -n  -z if [ -n "$PID" ]; then echo "PID is not empty" fi if[ -z "$ ...

  7. 解决阿里云数据库RDS报错The table '/home/mysql/data3015/tmp/#sql_13975_23' is full

    查询任何一条语句都显示 The table '/home/mysql/data3015/tmp/#sql_13975_23' is full 查看了下数据库利用磁盘空间没有满, 阿里云的处理方式: 1 ...

  8. 根据某个文件或文件夹自制rpm包

    首先安装打包软件# yum install rpmbuild  rpmdevtools -y 生成打包路径1.使用rpmdev-setuptree ,会自动在用户家目录生成一个rpmbuild的文件夹 ...

  9. information_schema系列四(跟踪,列约束,表和列)

    这个系列的文章主要是为了能够让自己了解MySQL5.7的一些系统表,统一做一下备注和使用,也希望分享出来让大家能够有一点点的受益. 1:KEY_COLUMN_USAGE 按照官方的解释,这个表描述的是 ...

  10. web测试特别点

    1.浏览器的后退按钮  提交表单一条已经成功提交的记录,back后再提交,看系统会如何处理.  检查多次使用back健的情况在有back的地方,back,回到原来的页面,再back,重复几次,看是否会 ...