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. 循序渐进Python3(十三) --1-- django之form表单

    在上一次的代码上做出进一步修改,使之能在页面上显示报错信息. views.py from django.shortcuts import render, HttpResponse from djang ...

  2. Jsp页面中的中文乱码问题解决

    Jsp页面中的中文乱码问题解决 在编写Jsp页面的时候,发现写入其中的中文在浏览器浏览的时候会出现乱码的情况. 出现乱码的原因分析: 因为页面中对自己的编码格式的声明和页面的实际编码格式不相同,而浏览 ...

  3. python3.3 MD5

    代码如下: # /usr/bin/python # -*- coding:utf-8 -*- import hashlib h=hashlib.md5() data = ' h.update(data ...

  4. saltstack系列(三)——zmq订阅/发布模式

    zmq订阅发布模式 server端代码: #coding=utf-8 ''''' 服务端,发布模式 ''' import zmq from random import randrange contex ...

  5. web服务器推送技术

    传统模式的 Web 系统以客户端发出请求.服务器端响应的方式工作.不能满足很多现实应用的需求,譬如: 监控系统:后台硬件温度.电压发生变化: 即时通信系统:其它用户登录.发送信息: 即时报价系统:后台 ...

  6. SpringBoot15 sell02 订单模块

    1 订单模块 1.1 MySQL数据表 订单模块涉及到两个数据表: 订单表:主要存储订单相关的基本信息 DROP TABLE IF EXISTS `order_master`; CREATE TABL ...

  7. jquery遮罩层

    (function () { //遮罩层实现 zhe zhao ceng kexb 2016.2.24 $.extend($.fn, { mask: function (msg, maskDivCla ...

  8. 奇妙的 Storage::url

    发现 这是我在做头像上传功能时发现的,下面是图片上传的业务逻辑. class AvatarController extends Controller { public function update( ...

  9. Linux下安装memcache PHP扩展

    [root@centos memcache-2.2.4]# wget http://pecl.php.net/get/memcache-2.2.4.tgz [root@centos memcache- ...

  10. 很好的QSqlDatabase问题说明,关于连接错误(转)

    QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connect ...