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.

Update (2015-02-10):
The signature of the C++ function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button  to reset your code definition.


题目标签:String

  题目给了我们一个 str,让我们把它 转换为 int。

  其中有很多违规的条件没有说明:

    正负的符号只能有0个 或者 1个;

    符号后面就应该是数字了,如果遇到不是数字的符号,返回目前为止合格的数字,不需要考虑后面的数字;

    如果数字overflow,大于MAX的要返回MAX,小于MIN 的要返回MIN;

    etc。

Java Solution:

Runtime beats 57.67%

完成日期:01/09/2017

关键词:String

关键点:考虑到所有违规情况

 class Solution
{
public int myAtoi(String str)
{
long res = 0; // the res number to return. Note: res to return should be long and cast it to int when return it at the end.
int sign = 1; // the sign before the number. default is 1 (positive).
int index = 0; // index for num string to go through. // Step 0: if parameter str is null or "", then return 0.
if(str.length() == 0 || str == null)
return 0; // Step 1: trim the whitespace.
str = str.trim(); // Step 2: check first char is '+' or '-', move the index by 1 and also sign value.
if(str.charAt(0) == '+')
index++;
else if(str.charAt(0) == '-')
{
index++;
sign = -1; // change the sign to -1 (negative).
} // Step 3: go through the str string.
for(; index<str.length(); index++)
{
// if this char is not a number char, then break. No matter there are more numbers after.
if(str.charAt(index) > '9' || str.charAt(index) < '0')
break; // add this char value into res.
res = res * 10 + (str.charAt(index) - '0'); // char - '0' is the correct int value. // check the num exceed the max or not.
if(res > Integer.MAX_VALUE) // res should be long because here res might be over Integer.max value.
break;
} // Step 4: depending on the sign and max or min value, return res.
if(res * sign >= Integer.MAX_VALUE)
return Integer.MAX_VALUE;
else if(res * sign <= Integer.MIN_VALUE)
return Integer.MIN_VALUE; // goes here meaning the res number doesn't exceed max and min integer value.
return (int)res * sign; // here need to cast res to int.
}
}

参考资料:N/A

LeetCode 题目列表 - LeetCode Questions List

题目来源:https://leetcode.com/

LeetCode 8. String to Integer (atoi) (字符串到整数)的更多相关文章

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

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

  2. [leetcode]8. String to Integer (atoi)字符串转整数

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

  3. 【LeetCode】String to Integer (atoi)(字符串转换整数 (atoi))

    这道题是LeetCode里的第8道题. 题目要求: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们 ...

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

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

  5. 【LeetCode】8. String to Integer (atoi) 字符串转换整数

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字符串转整数,atoi,题解,Leetcode, 力扣,P ...

  6. Leetcode8.String to Integer (atoi)字符串转整数(atoi)

    实现 atoi,将字符串转为整数. 该函数首先根据需要丢弃任意多的空格字符,直到找到第一个非空格字符为止.如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连续的数字组合起来,这部分字 ...

  7. 【LeetCode】8. String to Integer (atoi) 字符串转整数

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

  8. Leetcode 8 String to Integer (atoi) 字符串处理

    题意:将字符串转化成数字. 前置有空格,同时有正负号,数字有可能会溢出,这里用long long解决(leetcode用的是g++编译器),这题还是很有难度的. class Solution { pu ...

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

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

  10. 008 String to Integer (atoi) 字符串转换为整数

    详见:https://leetcode.com/problems/string-to-integer-atoi/description/ 实现语言:Java class Solution { publ ...

随机推荐

  1. [算法天天练] - C语言实现双向链表(一)

    双向链表是比较常见的,主要是在链表的基础上添加prev指针,闲话少说直接上代码吧(这个也是网上一个大神的思路,真心不错,条理清楚,逻辑缜密) 主要也是为了学习,贴上我所调试成功的代码(Linux环境下 ...

  2. 读《实战 GUI 产品的自动化测试》之:第二步,构建利于维护的自动化测试系统

    转载自:http://www.ibm.com/developerworks/cn/rational/r-cn-guiautotesting2/ 基石——IBM 框架简介 Rational Functi ...

  3. C语言中结构体大小计算

    1.普通结构体 struct student { char sex; char a; char b; int age; char name[100]; }; 该结构体大小为108 解答:1.先算str ...

  4. codeforces_459D_(线段树,离散化,求逆序数)

    链接:http://codeforces.com/problemset/problem/459/D D. Pashmak and Parmida's problem time limit per te ...

  5. Masonry 原理一

    Under the hood Auto Layout is a powerful and flexible way of organising and laying out your views. H ...

  6. MFC获取各窗口指针句柄

    MFC在很多的对话框操作中,我们经常要用到在一个对话框中调用另一个对话框的函数或变量.可以用如下方法来解决.   HWND hWnd=::FindWindow(NULL,_T("Sphere ...

  7. vue过渡 & 动画---进入/离开 & 列表过渡

    (1)概述 Vue 在插入.更新或者移除 DOM 时,提供多种不同方式的应用过渡效果.包括以下工具: 在 CSS 过渡和动画中自动应用 class 可以配合使用第三方 CSS 动画库,如 Animat ...

  8. ThinkPHP---AR模式

    [前言] 在之前学习框架时介绍过 (1)什么是框架? ①框架是一堆包含了常量.方法和类等代码集合: ②半成品应用,只包含了项目开发时的底层架构,并不包含业务逻辑: ③包含一些设计模式,例如单例模式,工 ...

  9. 【原】SMTP发送邮件

    1.下载class.phpmailer.php和class.smtp.php至公共库 2.编写发邮件的公共函数 function sendMail($param) { $config = C('THI ...

  10. 迭代器,生成器(generator)和Promise的“微妙”关系

    需要Promise源码版的朋友:传送链接 本文主要讲述(iterator)和生成器*/yield之间的联系和各自的用法,以及生成器的高配版本aysnc/await的使用. 大纲: 迭代器(iterat ...