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.

思路:INT最大是2147483647,最小是-2147483648。所以要尤其注意-2147483648取相反数是得不到2147483648的。

class Solution {
public:
int myAtoi(string str) {
int ret = ;
int i = ;
bool pos = true; while(str[i] == ' ') i++; //neglect the spaces at the start
if(str[i] == '+') i++;
else if(str[i] == '-') {
pos = false;
i++;
} while(str[i] != '\0'){
if(str[i] > '' || str[i] <'') break; //if invalid digit, stop scanning and return the current result
if(ret > || (ret == && str[i] - '' > )){ //if overflow, return the extreme value
if(pos) return INT_MAX;
else return INT_MIN;
}
ret = ret * + str[i] - '';
i++;
}
if(pos) return ret;
else return (-ret);
}
};

8.String to Integer (atoi) (INT; Overflow)的更多相关文章

  1. 【leetcode】String to Integer (atoi)

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

  2. No.008 String to Integer (atoi)

    8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...

  3. leetcode第八题 String to Integer (atoi) (java)

    String to Integer (atoi) time=272ms   accepted 需考虑各种可能出现的情况 public class Solution { public int atoi( ...

  4. 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 ...

  5. String to Integer (atoi) - 字符串转为整形,atoi 函数(Java )

    String to Integer (atoi) Implement atoi to convert a string to an integer. [函数说明]atoi() 函数会扫描 str 字符 ...

  6. Kotlin实现LeetCode算法题之String to Integer (atoi)

    题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 class ...

  7. LeetCode--No.008 String to Integer (atoi)

    8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...

  8. leetcode-algorithms-8 String to Integer (atoi)

    leetcode-algorithms-8 String to Integer (atoi) Implement atoi which converts a string to an integer. ...

  9. LeetCode: String to Integer (atoi) 解题报告

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

随机推荐

  1. js字符串转换成数字,数字转换成字符串

    转自网络,忘记出处了. js字符串转换成数字 将字符串转换成数字,得用到parseInt函数. parseInt(string) : 函数从string的开始解析,返回一个整数. 举例:parseIn ...

  2. mysql响应时间超时排查

    背景: 数据库运营环境,zabbix mysql响应时间告警,响应时间超时 zabbix监控 tcprstart 直接抓包响应时间看到每5秒钟就一次,与zabbix监控一致 [root@slave1( ...

  3. 第5章 进程环境(1)_进程结构(task_struct)

    1. 进程的概念和进程结构 1.1 进程 (1)程序(program):是一些保存在磁盘上有序指令的集合,是存放在磁盘文件中的可执行文件.但没有任何执行的概念,它是静态的. (2)进程(process ...

  4. HTC Vive的定位技术

    Lighthouse空间定位,chaperone系统避免实际障碍物 HTC vive所用的Lighthouse技术属于激光定位技术,Oculus Rift以及索尼PlayStation VR所用的定位 ...

  5. kudu架构(转)

    特点:   High availability(高可用性).Tablet server 和 Master 使用 Raft Consensus Algorithm 来保证节点的高可用,确保只要有一半以上 ...

  6. SparkStreaming 运行原理与核心概念

    SparkStreaming 运行原理 sparkstreaming 的高层抽象DStream Dstream与RDD的关系 Batch duration

  7. jmeter测试20个QPS下的响应时间-设置QPS限制

    添加--->定时器--->Constant Throughput Timer Constant Throughput Timer 的主要属性介绍: 名称:定时器的名称 Target thr ...

  8. ansible的安装与使用

    ansible的特点: 1. 基于ssh运行 2. 无需客户端 安装ansible 这里提供四种安装方式,根据自己的需要任选一种即可 1.1使用yum安装 yum install epel-relea ...

  9. 0_Simple__simpleCooperativeGroups

    ▶ 协作组,CUDA9.0 的新特性 ▶ 源代码,如何获得协作组的编号? #include <stdio.h> #include "cuda_runtime.h" #i ...

  10. vsphere使用笔记

    一,安装虚拟机,需要使用vsphere client上传镜像,提示错误:   vsphere client 上传文件:Failed to log into NFC Server   , 解决办法:用浏 ...