String to Integer
Implement function atoi
to convert a string to an integer.
If no valid conversion could be performed, a zero value is returned.
If the correct value is out of the range of representable values, INT_MAX(2147483647) or INT_MIN (-2147483648) is returned.
"10" => 10
"-1" => -1
"123123123123123" => 2147483647
"1.0" => 1
public class Solution {
public int myAtoi(String str) {
if (str == null || str.length() == )
return ;
str = str.trim();
if (str.length() == && (str.equals("+") || str.equals("-") || str.equals(".") || str.equals(""))) {
return ;
} boolean isPositive = true;
long current = ; for (int i = ; i < str.length(); i++) {
if (i == && str.charAt(i) == '+') {
continue;
} else if (i == && str.charAt(i) == '-') {
isPositive = false;
} else if (str.charAt(i) >= '' && str.charAt(i) <= '') {
current = current * + str.charAt(i) - '';
if (isPositive && current > Integer.MAX_VALUE)
return Integer.MAX_VALUE;
if (!isPositive && -current < Integer.MIN_VALUE)
return Integer.MIN_VALUE;
} else {
break;
}
} if (!isPositive) {
current = -current;
} return (int) current;
}
}
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. ...
随机推荐
- Oracle创建表格报ORA-00906:缺失左括号错误解决办法
来源于:http://www.linuxidc.com/Linux/2013-06/85297.htm 解决办法: create table myTable(id number(5,2),name v ...
- ansible 的组件inventory
P44 Ansible 的默认的inventory的是一个静态的ini格式的文件/etc/ansible/hosts. 我们还可以通过ansible_hosts环境变脸指定或者运行ansible和an ...
- shell中一维数组值得获取
(1)数组的定义 root@tcx4440-03:~# a=(1 2 3 4) root@tcx4440-03:~# echo ${a[1]}2 root@tcx4440-03:~# a[0]=1ro ...
- 【CodeForces 626E】Simple Skewness
题意 给出n个数的集合,求一个 (平均数-中位数)最大 (偏度最大)的子集,输出子集元素个数和各个元素(任意顺序). 分析 因为是子集,所以不一定是连续的序列.然后我们有下面几个结论. 1.最大偏度一 ...
- 【CodeForces 613A】Peter and Snow Blower
题 题意 给出原点(不是(0,0)那个原点)的坐标和一个多边形的顶点坐标,求多边形绕原点转一圈扫过的面积(每个顶点到原点距离保持不变). 分析 多边形到原点的最小距离和最大距离构成的两个圆之间的圆环就 ...
- 【HDU 4925】BUPT 2015 newbie practice #2 div2-C-HDU 4925 Apple Tree
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=102419#problem/C Description I’ve bought an or ...
- 14.Android之Layout布局学习
Android布局主要有5种,接下来学习总结下. 1) 最常见的线性布局 LinearLayout 线性布局是Android布局中最简单的布局,也是最常用,最实用的布局. android:orient ...
- VMware 11安装Mac OS X 10.11.5虚拟机以及优化心得
随着苹果WWC大会退出了MAC最新版的10.11.5,本着一颗“极客”的心情,在第一时间用VMWARE虚拟机装上了.然后各种卡顿这里分享一下优化mac虚拟机的心得. 1 从Dock上移除Dashboa ...
- 不要在初始化方法和dealloc方法中使用Accessor Methods
苹果在<Advanced Memory Management Programming Guide>指出: Don’t Use Accessor Methods in Initializer ...
- MAC Java 开发环境配置
JDK Oracle官网 -> Download -> Java for Developers -> Java SE Downloads -> Java Platform (J ...