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

题目描述

实现atoi函数,将一个字符串转化为数字

测试样例

Input: "42"
Output: 42 Input: " -42"
Output: -42 Input: "4193 with words"
Output: 4193 Input: "words and 987"
Output: 0

详细分析

这道题的corner cases非常多,请务必确保下面cases都能通过的情况下再提交。

"42"
"words and 987"
"-91283472332"
"0-1"
"-000000000000001"
" 0000000000012345678"
"10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000522545459"
"-2147483647"
"-2147483648"
"2147483648"
"2147483649"
""
"7"
" +0 123"

算法实现

class Solution {
public:
string trim(const std::string&str){
string nstr;
int i=0;
while(isspace(str[i])){
i++;
} for(;i<str.length();i++){
if(isspace(str[i])){
break;
}
nstr +=str[i];
}
return nstr;
} int myAtoi(string str) {
str = trim(str);
if(str.length()==0 || (str[0]!='+'&&str[0]!='-'&& !isdigit(str[0]))){
return 0;
} int i=0; //consume sign char
if(str[0] =='+' || str[0]=='-'){
i++;
}
string nstr;
while(isdigit(str[i])){
nstr+=str[i];
i++;
}
if(nstr.length()==0){
return 0;
}
i=0;
// consume meaningless zeros
while(nstr[i]=='0'){
i++;
}
nstr = nstr.substr(i);
long long result = 0L;
unsigned long long exp = 1;
for(int k=nstr.length()-1;k>=0;k--){
result += ((int)(nstr[k]-'0'))*exp; if(exp> numeric_limits<int>::max()){
return str[0]=='-'?numeric_limits<int>::min():numeric_limits<int>::max();
}
exp*=10;
if(result> numeric_limits<int>::max()){
return str[0]=='-'?numeric_limits<int>::min():numeric_limits<int>::max();
}
}
return str[0]=='-'?-result:result;
}
};

Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)的更多相关文章

  1. leetcode:String to Integer (atoi)

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

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

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

  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] 8. String to Integer (atoi) (Medium)

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

  5. 【leetcode】String to Integer (atoi)

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

  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] 8. String to Integer (atoi) 字符串转为整数

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

  8. LeetCode——8. String to Integer (atoi)

    一.题目链接:https://leetcode.com/problems/string-to-integer-atoi/ 二.题目大意: 实现一个和C语言里atoi具有相同功能的函数,即能够把字符串转 ...

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

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

随机推荐

  1. 监控和安全运维 1.7 nagios配置邮件告警

    8. 配置邮件告警 服务端 vim /etc/nagios/objects/contacts.cfg 增加: define contact{ contact_name use generic-cont ...

  2. Linux的基本指令--其他命令

    一 . 终端翻页: shift-pageup shift-pagedown 二 . 看手册:man man man 2 read 查看read系统函数的man page(在第二个section中,表示 ...

  3. flask+jsonp跨域前后台交互(接口初体验)

    1 # -*- coding: utf-8 -*- 2 from flask import Flask, jsonify 3 import psutil, time,json 4 5 app = Fl ...

  4. Priceless Notes

    [Priceless Notes] 1.人类对价格的绝对值没有准确的判断,但是价格或物体的相对值有较准确的判断. 2.物理强度与主观体验的关联成幂曲线.如60瓦的灯会让人觉得亮,但要让人觉得2倍亮的话 ...

  5. Springboot21 整合redis、利用redis实现消息队列

    1 前提准备 1.1 创建一个springboot项目 技巧01:本博文基于springboot2.0创建 1.2 安装redis 1.2.1 linux版本 参考博文 1.2.2 windows版本 ...

  6. 我搭建大数据Hadoop完全分布式环境遇到的坑---hadoop: command not found

    搭建大数据hadoop环境,遇到很多问题,这里记录一部分,以备以后查看. [遇到问题].在安装配置完hadoop以后,需要格式化namenode,输入指令:hadoop namenode -forma ...

  7. while 循环和do while循环

    while循环是先检测条件符合不符合,符合才执行循环体内容,不符合就跳过while循环. 就和一个房间有两个门,一个前门,一个后门,while循环是当你进入前门的时候有人会检查你的身份,只有身份符合条 ...

  8. chrome浏览器-Toolbar工具条不显示

    来源于<sencha touch 权威指南> ------------------------------------ 工具条按钮在chrome下不显示,不知是不支持还是代码有问题.app ...

  9. web大文件上传控件-监控fd_create流程-Xproer.HttpUploader6

      监控fd_create流程 1.打开ie,f12 2.启动网络监控 点击开始捕获 上传文件夹,然后查看监控 将监控信息转到详细视图

  10. C++中的Trivial 、POD、non-POD和Standard Layout概念

    POD types non-POD types Standard Layout types A Formal Definition Informally, a standard layout clas ...