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. ...
随机推荐
- C# 写 LeetCode easy #28 Implement strStr()
28.Implement strStr() Implement strStr(). Return the index of the first occurrence of needle in hays ...
- 001.SQLServer高可用简介
一 SQLServer高可用集群相关概念 1.1 Windows故障转移群集 Windows故障转移群集是由多个服务器组成的共同提供某高可用服务,该服务用于防止单台服务器故障导致服务失效.故障转移群集 ...
- nodebrew
创建: 2019/05/30 完成: 2019/05/30 安装 安装 curl -L git.io/nodebrew | perl - setup 更新nodebrew nodebrew sel ...
- (PHP)redis Hash(哈希)操作
/** * * Hash操作 * 哈希操作 * 可理解为数据库操作 * */ //为user表中的字段赋值.成功返回1,失败返回0.若user表不存在会先创建表再赋值,若字段已存在会覆盖旧值. $re ...
- python 基于 wordcloud + jieba + matplotlib 生成词云
词云 词云是啥?词云突出一个数据可视化,酷炫.以前以为很复杂,不想python已经有成熟的工具来做词云.而我们要做的就是准备关键词数据,挑一款字体,挑一张模板图片,非常非常无脑.准备好了吗,快跟我一起 ...
- 系统:Centos 7.2 内核3.10.0-327.el7.x86_64 # 内核需要高于2.6.32
系统:Centos 7.2 内核3.10.0-327.el7.x86_64 # 内核需要高于2.6.32 Drbd : 192.168.8.111:node1/dev/drdb0 /mydeta 19 ...
- (四)从输入URL到页面加载发生了什么
一.从输入URL到页面加载发生了什么 1.在浏览器中输入URL 如:https://www.cnblogs.com/loveapple/ URL分成协议.地址.路径三部分 协议:http.https. ...
- Codeforces Round #565 (Div. 3) A. Divide it!
链接: https://codeforces.com/contest/1176/problem/A 题意: You are given an integer n. You can perform an ...
- D. Little Artem and Dance
题目链接:http://codeforces.com/problemset/problem/669/D D. Little Artem and Dance time limit per test 2 ...
- C. Glass Carving 正着做或者倒着做都可以
http://codeforces.com/problemset/problem/527/C 这题总体思路就是,每画一条线,然后就找到x间距的最max值和y间距的最max值,相乘就是当前的ans 那么 ...