LeetCode 8. String to Integer (atoi) (字符串到整数)
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) (字符串到整数)的更多相关文章
- [LeetCode] 8. String to Integer (atoi) 字符串转为整数
Implement atoi which converts a string to an integer. The function first discards as many whitespace ...
- [leetcode]8. String to Integer (atoi)字符串转整数
Implement atoi which converts a string to an integer. The function first discards as many whitespace ...
- 【LeetCode】String to Integer (atoi)(字符串转换整数 (atoi))
这道题是LeetCode里的第8道题. 题目要求: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们 ...
- [LeetCode] String to Integer (atoi) 字符串转为整数
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- 【LeetCode】8. String to Integer (atoi) 字符串转换整数
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字符串转整数,atoi,题解,Leetcode, 力扣,P ...
- Leetcode8.String to Integer (atoi)字符串转整数(atoi)
实现 atoi,将字符串转为整数. 该函数首先根据需要丢弃任意多的空格字符,直到找到第一个非空格字符为止.如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连续的数字组合起来,这部分字 ...
- 【LeetCode】8. String to Integer (atoi) 字符串转整数
题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...
- Leetcode 8 String to Integer (atoi) 字符串处理
题意:将字符串转化成数字. 前置有空格,同时有正负号,数字有可能会溢出,这里用long long解决(leetcode用的是g++编译器),这题还是很有难度的. class Solution { pu ...
- LeetCode OJ String to Integer (atoi) 字符串转数字
#include <iostream> #include <assert.h> using namespace std; int ato(const char *str) { ...
- 008 String to Integer (atoi) 字符串转换为整数
详见:https://leetcode.com/problems/string-to-integer-atoi/description/ 实现语言:Java class Solution { publ ...
随机推荐
- JavaScript开发心得--如何传递某行数据给下一页
1, 应用场景 在某个html页面显示一批数据,如20个用户的名称.年龄等,每行都要一个编辑按钮,点击编辑后,将此行数据带入某个专门的编辑页进行显示,修改后保存. 问题是 点击编辑按钮后,如何得知要编 ...
- 编写Java脚本统计工程代码总行数
在新公司工作将近一年了,一直独自一人负责服务端集群的运维和代码的编写.不知不觉从一个Project发展到了七八个Project. 看着越来越多的代码,今天突然想统计一下一共写了多少代码.[这里只统计完 ...
- Stanford coursera Andrew Ng 机器学习课程第四周总结(附Exercise 3)
Introduction Neural NetWork的由来 时,我们可以对它进行处理,分类.但是当特征数增长为时,分类器的效率就会很低了. Neural NetWork模型 该图是最简单的神经网络, ...
- jboss解决ip访问受限问题
jboss启动后,localhost可以访问,127.0.0.1可以访问,但是内网ip却访问不了,比如ip是192.168.1.2,这个192.168.1.2就访问不到web页面 解决方案: jbos ...
- Xcode git 忽略user interface state文件
退出xcdoe, 打开终端(Terminal),进入到你的项目目录下 在终端输入如下代码 git rm --cached *.xcuserstate git commit -m "Remov ...
- centos7进入救援模式,修复错误配置
因某些修改操作,导致系统重启后无法正常启动,此时可进入救援模式,修复错误配置即可. OS:centos 7 1.重启系统后,进入grup引导页面,选中第一项然后按“e” 进入编辑模式: 2.通过↓键找 ...
- Centos7安装MySQL5.7(yum)
本人尝试过使用源码安装方式,那叫一个头疼,各种问题,于是采用yum方式安装,没想到如此简单: 此服务器是刚买的,所以以前没有安装过mysql,如果以前安装过mysql的,好像要卸载干净再安装(其实我也 ...
- Centos6文本安装教程
Centos6.4文本方式安装 虚拟机中文本安装(内存512),内存大于512默认为图形安装 1.选择安装媒体,在vbox中选skip跳过 2.选择安装语言(chinese(simplifired)简 ...
- Mac 当xampp里mysql无法启动的解决办法
sudo /Applications/XAMPP/xamppfiles/bin/mysql.server start
- vue-cli中src/main.js 的作用
// The Vue build version to load with the `import` command // (runtime-only or standalone) has been ...