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. ...
随机推荐
- BZOJ solve 100 纪念
按照xiaoyimi立下的flag是不是该去表白啦--可惜并没有妹子
- Erlang第三课 ---- 创建和使用module
----------------小技巧----------------------------- 因为这一课开始,我们要使用Erlang文件操作,所以,我们期待启动shell的时候,当前目录最好是是我 ...
- 一个共通的viewModel搞定所有的编辑页面-经典ERP录入页面(easyui + knockoutjs + mvc4.0)
http://www.cnblogs.com/xqin/archive/2013/06/06/3120887.html 前言 我写代码喜欢提取一些共通的东西出来,之前的一篇博客中说了如何用一个共通的v ...
- 软件开发过程中的审查 (Review)
http://blog.csdn.net/horkychen/article/details/5035769 软件开发过程中的审查 (Review) 希望别人做些什么->定义出流程 希望别人 ...
- c# 闭包 小例
class Program { static void fnnn() { string[] k = new string[] { "x", "y", " ...
- jquery------显示加载的js时出现中文乱码解决方法
方法: 把my.js文件复制出来,用记事本打开,再另存为的时候设置编码格式为utf-8即可
- sparkR操作HDFS上面的CSV文件
./bin/sparkR --packages com.databricks:spark-csv_2.10:1.3.0 --master yarn hdfs://master:9000/tmp/dem ...
- linux 访问tomcat 管理页面时 You are not authorized to view this page 403(真实可用)
ava代码 收藏代码 You are not authorized to view this page. If you have not changed any configuration files ...
- 转:Java NIO系列教程(五) 通道之间的数据传输
在Java NIO中,如果两个通道中有一个是FileChannel,那你可以直接将数据从一个channel(译者注:channel中文常译作通道)传输到另外一个channel. transferFro ...
- [Winform]一个简单的账户管理工具
最近一直觉得注册的账户越来越多,帐号密码神马的容易弄混.自己就折腾了一个简单的账户管理工具,其实实现也挺简单,将每个账户的密码及相关密码提示信息,经aes算法加密之后保存到数据库,当前登录用户可以查询 ...