LeetCode----8. String to Integer (atoi)(Java)
package myAtoi8;
/*
* 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.
*/ public class Solution {
public static int myAtoi(String str) {
StringBuilder sb=new StringBuilder();
int flag=0;
if(str.length()==0)
return 0;
else{
for(int i=0;i<str.length();i++){
if(str.charAt(i)<'0'||str.charAt(i)>'9')
if(str.charAt(i)=='0'||str.charAt(i)==' ')
if (sb.length()==0)
continue;
else
break;
else if (str.charAt(i)=='-')
if (flag==0){
flag=-1;
sb.append('0');
}
else
break;
else if (str.charAt(i)=='+')
if (flag==0){
flag=1;
sb.append('0');
}
else
break;
else
break;
else
sb.append(str.charAt(i));
}
}
if(sb.length()==0)
return 0;
else if(sb.length()>12)
str=sb.substring(sb.length()-12);
else
str=sb.toString();
if (flag==-1)
str="-"+str;
long result=Long.parseLong(str);
System.out.println(result);
if (result>2147483647)
return 2147483647;
else if (result<-2147483648)
return -2147483648;
else
return (int)result; }
public static void main(String[] args){
String str1="";//
String str2="+";//
String str3=" +45555";//
String str4=" -45555";//-45555
String str5="2147483647";//
String str6="2147483648";//
String str7="+-2";//
String str8="-+2";//
String str9="++2";//
String str10="-2147483648";//-2147483648
String str11=" - 321";//
String str12=" -11919730356x";//-2147483648
String str13="1234567890123456789012345678901234567890";//
System.out.println(myAtoi(str1));
System.out.println(myAtoi(str2));
System.out.println(myAtoi(str3));
System.out.println(myAtoi(str4));
System.out.println(myAtoi(str5));
System.out.println(myAtoi(str6));
System.out.println(myAtoi(str7));
System.out.println(myAtoi(str8));
System.out.println(myAtoi(str9));
System.out.println(myAtoi(str10));
System.out.println(myAtoi(str11));
System.out.println(myAtoi(str12));
System.out.println(myAtoi(str13)); }
}
LeetCode----8. String to Integer (atoi)(Java)的更多相关文章
- Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)
Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...
- LeetCode【8】. String to Integer (atoi) --java实现
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- 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 ...
- 【leetcode】String to Integer (atoi)
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- [leetcode] 8. String to Integer (atoi) (Medium)
实现字符串转整形数字 遵循几个规则: 1. 函数首先丢弃尽可能多的空格字符,直到找到第一个非空格字符. 2. 此时取初始加号或减号. 3. 后面跟着尽可能多的数字,并将它们解释为一个数值. 4. 字符 ...
- Leetcode 8. String to Integer (atoi)(模拟题,水)
8. String to Integer (atoi) Medium Implement atoi which converts a string to an integer. The functio ...
- [LeetCode][Python]String to Integer (atoi)
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/string- ...
- 【JAVA、C++】 LeetCode 008 String to Integer (atoi)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- Java [leetcode 8] String to Integer (atoi)
问题描述: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ...
随机推荐
- 2016huas暑假集训训练题 G-Who's in the Middle
题目链接:http://acm.hust.edu.cn/vjudge/contest/121192#problem/G 此题大意是给定一个数n 然后有n个数 要求求出其中位数 刚开始以为是按数学中的 ...
- 解决Android调用https服务API时出错的问题
今天同事告诉我说他的应用调用我开发的API报异常了,原因跟SSL有关系,因为之前调试一直调用的是HTTP服务API,今天调试HTTPS服务API时报错了,并且找到了一篇文章让我看了一眼,文章中提到了W ...
- Empire C:Basic 2
作为人与计算机沟通的媒介,C语言给我们呈现了:printf.scanf.以及缓冲区. 1.printf("%d",a) %d:d是decimal base(十进制)的开头字母,意思 ...
- Python脚本模拟登录网页之ZiMuZu篇
ZiMuZu.tv这个网站喜欢看电影看美剧的人一定都熟悉. 这个网站原先的升级策略是每天登陆网站, 然后去一个"每日签到"的页面点击一个签到按钮, 以实现帐号等级的升级. 之前网上 ...
- 【应用笔记】【AN004】VB环境下基于RS-485的4-20mA电流采集
版本:第一版作者:周新稳 杨帅 日期:20160226 =========================== 本资料高清PDF 下载: http://pan.baidu.com/s/1c1uuhLQ ...
- NEC学习 ---- 模块 -文本圆角背景导航
下图是效果图: 然后, 左右两边的圆角图片和背景图片如下 (因为截图工具的原因, 可能图片不是很清晰. 这个图片有4个部分, 分别是中间的背景图, 左右圆角以及栏目分隔白线) 思路: 利用inline ...
- 【ionic】微信表情设置教程
NO.0 前提你的下载我的App :超级逗表情 Andorid:http://fir.im/chaojidbq Ios:https://github.com/apanly/chaojidbq (源码, ...
- [troubleshoot][archlinux][X] GPU HANG
前言:如下内容已经是在hang完大概半个多月后了,当时想写,一直没过来写,写blog果然也是已经花费时间的事情. 最近一直在休假,电脑的使用频率也不多.后来还是为了生活,不情愿的去开始上班了,上班的第 ...
- MVC下载文件方式
MVC下载文件方式 http://www.cnblogs.com/liang--liang/archive/2012/10/20/2732745.html 方式一: public FileStream ...
- linux下tar、zip等压缩、解压命令
.tar解包:tar xvf FileName.tar打包:tar cvf FileName.tar DirName(注:tar是打包,不是压缩!)-------------------------- ...