8. String to Integer
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 型。
1. null or empty string
2. white spaces
3. +/- sign
4. calculate real value
5. handle min & max
public int atoi(String str) {
if (str == null || str.length() < 1)
return 0;
// trim white spaces
str = str.trim();
char flag = '+';
// check negative or positive
int i = 0;
if (str.charAt(0) == '-') {
flag = '-';
i++;
} else if (str.charAt(0) == '+') {
i++;
}
// use double to store result
double result = 0;
// calculate value
while (str.length() > i && str.charAt(i) >= '0' && str.charAt(i) <= '9') {
result = result * 10 + (str.charAt(i) - '0');
i++;
}
if (flag == '-')
result = -result;
// handle max and min
if (result > Integer.MAX_VALUE)
return Integer.MAX_VALUE;
if (result < Integer.MIN_VALUE)
return Integer.MIN_VALUE;
return (int) result;
}
class Solution:
# @return an integer
def atoi(self, str):
str = str.strip()
if not str:
return 0 MAX_INT = 2147483647
MIN_INT = -2147483648
ret = 0
overflow = False
pos = 0
sign = 1 if str[pos] == '-':
pos += 1
sign = -1
elif str[pos] == '+':
pos += 1 for i in range(pos, len(str)):
if not str[i].isdigit():
break
ret = ret * 10 + int(str[i])
if not MIN_INT <= sign * ret <= MAX_INT:
overflow = True
break if overflow:
return MAX_INT if sign == 1 else MIN_INT
else:
return sign * ret
# 正则表达式
class Solution:
# @return an integer
def atoi(self, str):
str = str.strip()
str = re.match(r'^[+-]?\d+', str).group()
MAX_INT = 2147483647
MIN_INT = -2147483648 try:
ret = int(str)
if ret > MAX_INT:
return MAX_INT
elif ret < MIN_INT:
return MIN_INT
else:
return ret
except:
return 0
8. String to Integer的更多相关文章
- 【leetcode】String to Integer (atoi)
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- No.008 String to Integer (atoi)
8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...
- 【LeetCode】7 & 8 - Reverse Integer & String to Integer (atoi)
7 - Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Notic ...
- leetcode第八题 String to Integer (atoi) (java)
String to Integer (atoi) time=272ms accepted 需考虑各种可能出现的情况 public class Solution { public int atoi( ...
- leetcode day6 -- String to Integer (atoi) && 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 ...
- String to Integer (atoi) - 字符串转为整形,atoi 函数(Java )
String to Integer (atoi) Implement atoi to convert a string to an integer. [函数说明]atoi() 函数会扫描 str 字符 ...
- Kotlin实现LeetCode算法题之String to Integer (atoi)
题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 class ...
- String与Integer问题
今天分享一下关于最近面试的问题,临近春节,而我在茫茫人海中奔波,今天面试了来到了中关村科技园,挺气派的,之前也是在外面看看,今天就去了,心里有点激动,恰好,正好赶上了上班时,看见它们的努力,我感到再累 ...
- LeetCode--No.008 String to Integer (atoi)
8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...
- leetcode-algorithms-8 String to Integer (atoi)
leetcode-algorithms-8 String to Integer (atoi) Implement atoi which converts a string to an integer. ...
随机推荐
- 使用imp命令和exp命令对oracle数据库进行导入导出操作
命令说明 imp命令和exp命令需要在cmd命令窗口下执行,并且需要在系统环境变量中配置imp,exp命令所在目录 导出命令 --全库导出 exp system/systempassword@orcl ...
- java加密MD5实现及密码验证
package test; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; impor ...
- 【原创】智能合约安全事故回顾分析(1):The Dao事件
首先需要说明的一点是,这个世界上没有绝对安全的技术.在区块链发展的十年里,各种基于区块链的数字货币引发的安全事故层出不穷,这些安全威胁主要来源有三个方面: 自身安全机制的问题,类似智能合约. 生态安全 ...
- C语言经典算法100例(三)
1.河内之塔 说明河内之塔(Towers of Hanoi)是法国人M.Claus(Lucas)于1883年从泰国带至法国的,河内为越战时北越的首都,即现在的胡志明市:1883年法国数学家 Edoua ...
- JAVA String.format()的使用
常规类型的格式化 String类的format()方法用于创建格式化的字符串以及连接多个字符串对象.熟悉C语言的同学应该记得C语言的sprintf()方法,两者有类似之处.format()方法有两种重 ...
- Python的自增运算与Python变量的浅析
一.关于Python的自增运算 学了C/C++后再学习Python,不自觉地就打出了自增运算符++,但是发现Python解释器不认识,查了下资料,发现Python中没有这个运算符.这里暂时不探讨自增运 ...
- web综合案例02
web综合案例02 web综合案例02 web综合案例02 ... ... 内容待添加
- Educational Codeforces Round 48 (Rated for Div. 2) D 1016D Vasya And The Matrix (构造)
D. Vasya And The Matrix time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- k8s 更新应用程序
参考:https://kubernetes.io/docs/tutorials/kubernetes-basics/ 更新应用程序 用户希望应用程序始终可用,开发人员每天需要部署几次新版本的应用程序. ...
- 如何顺畅使用sourcetree可视化工具
http://www.360doc.com/content/17/0711/10/11253639_670493403.shtml sourcetree软件下载 下载地址:https://www.so ...