题目描述:

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.

解题思路:

这道题简直让人抓狂!试了许多次都没有通过,原因是要考虑多种情况。主要考虑的因数有:

  1. 要考虑前几个或全部字符都是空白字符的情况;
  2. 要考虑第一个字符是+,-,还是0-9的字母,或是其他字符;
  3. 要考虑有没有值得溢出,尤其是最小值溢出;
  4. 若中间有非数字字符,要返回之前的数字,如“123a34”要返回123;
  5. 若以若干个0开始,则0要忽略,如“-0000234”要返回-234

找出这几种情况之后,就代码就不难写了。总之这道题AC率很低的原因并不是逻辑有多难,而是很难考虑到全部的情况。

具体代码:

public class Solution {
public static int myAtoi(String str) {
//取出两边空白字符
str=str.trim();
if(str==null||str.length()==0)
return 0;
if(str.length()==1){
if(str.charAt(0)>='0' && str.charAt(0)<='9')
return Integer.valueOf(str);
else
return 0;
}
//第一个字符是'+
if(str.charAt(0)=='+'){
String s = str.substring(1);
if(isValid(s)){
s=fun(s);
if(s==null)
return 0;
MyCompatator m = new MyCompatator();
if(m.compare(s, ""+Integer.MAX_VALUE)>=0){
return Integer.MAX_VALUE;
}
else{
return Integer.valueOf(s);
}
}
else{
s=fun1(s);
if(s==null)
return 0;
s=fun(s);
if(s==null)
return 0;
MyCompatator m = new MyCompatator();
if(m.compare(s, ""+Integer.MAX_VALUE)>=0){
return Integer.MAX_VALUE;
}
else{
return Integer.valueOf(s);
}
}
}
//第一个字符是'-
else if(str.charAt(0)=='-'){
String s = str.substring(1);
if(isValid(s)){
s=fun(s);
if(s==null)
return 0;
MyCompatator m = new MyCompatator();
StringBuilder sb =new StringBuilder(""+Integer.MIN_VALUE);
sb.deleteCharAt(0);
String ss=sb.toString();
if(m.compare(s, ss)>=0)
return Integer.MIN_VALUE;
else{
return Integer.valueOf(str);
}
}
else{
s=fun1(s);
if(s==null)
return 0;
s=fun(s);
if(s==null)
return 0;
MyCompatator m = new MyCompatator();
StringBuilder sb =new StringBuilder(""+Integer.MIN_VALUE);
sb.deleteCharAt(0);
String ss=sb.toString();
if(m.compare(s, ss)>=0)
return Integer.MIN_VALUE;
else{
return Integer.valueOf("-"+s);
}
}
}
//第一个字符是数字
else if(str.charAt(0)>='0' && str.charAt(0)<='9'){
if(isValid(str)){
str=fun(str);
if(str==null)
return 0;
MyCompatator m = new MyCompatator();
if(m.compare(str, ""+Integer.MAX_VALUE)>=0){
return Integer.MAX_VALUE;
}
else{
return Integer.valueOf(str);
}
}
else{
str=fun1(str);
if(str==null)
return 0;
str=fun(str);
if(str==null)
return 0;
MyCompatator m = new MyCompatator();
if(m.compare(str, ""+Integer.MAX_VALUE)>=0){
return Integer.MAX_VALUE;
}
else{
return Integer.valueOf(str);
}
}
}
//第一个字符是其他数字则出错
else{
return 0;
} }
//判断字符串是否是由数字组成的
public static boolean isValid(String s){
//return s.matches("[1-9][0-9]*");
return s.matches("[0-9]+");
}
//将数字串开始的0全部去掉
public static String fun(String s){
int index=0;
boolean key=false;
for(index=0;index<s.length();index++){
if(s.charAt(index)!='0'){
key=true;
break;
}
}
if(key){
return s.substring(index);
}
else{
return null;
}
}
//截取第一个不是数字字符之前的数字
public static String fun1(String s){
int index=0;
for(index=0;s.charAt(index)>='0'&&s.charAt(index)<='9';index++){ }
if(index==0)
return null;
return s.substring(0,index);
}
}
class MyCompatator implements Comparator<String>{ @Override
public int compare(String s1, String s2) {
if(s1.length()>s2.length()){
return 1;
}
else if(s1.length()<s2.length()){
return -1;
}
else{ for(int i=0;i<s1.length();i++){
if(s1.charAt(i) - s2.charAt(i) >0)
return 1;
else if(s1.charAt(i) - s2.charAt(i) <0)
return -1;
else
;
}
return 0;
}
} }

【leetcode】8. String to Integer (atoi)的更多相关文章

  1. 【LeetCode】8. String to Integer (atoi) 字符串转换整数

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字符串转整数,atoi,题解,Leetcode, 力扣,P ...

  2. 【LeetCode】8. String to Integer (atoi) 字符串转整数

    题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...

  3. 【LeetCode】008. String to Integer (atoi)

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  4. 【一天一道LeetCode】#8. String to Integer (atoi)

    一天一道LeetCode系列 (一)题目 Implement atoi to convert a string to an integer. Hint: Carefully consider all ...

  5. 【LeetCode】字符串 string(共112题)

    [3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...

  6. 《LeetBook》leetcode题解(8): String to Integer (atoi) [E]——正负号处理

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  7. 【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 ...

  8. 【leetcode❤python】 8. String to Integer (atoi)

    #-*- coding: UTF-8 -*-#需要考虑多种情况#以下几种是可以返回的数值#1.以0开头的字符串,如01201215#2.以正负号开头的字符串,如'+121215':'-1215489' ...

  9. 【LeetCode】7、Reverse Integer(整数反转)

    题目等级:Easy 题目描述: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 O ...

随机推荐

  1. Codeforces Round #192 (Div. 1) C. Graph Reconstruction 随机化

    C. Graph Reconstruction Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/3 ...

  2. MKMapView的内存释放问题

    MKMapView的内存释放问题 by 伍雪颖 - (void)dealloc { self.mapView.showsUserLocation = NO; self.mapView.userTrac ...

  3. 算法优化:rgb向yuv的转化最优算法,快得让你吃惊!

    朋友曾经给我推荐了一个有关代码优化的pdf文档<让你的软件飞起来>,看完之后,感受颇深.为了推广其,同时也为了自己加深印象,故将其总结为word文档.下面就是其的详细内容总结,希望能于己于 ...

  4. iOS开发——高级UI&带你玩转UITableView

    带你玩装UITableView 在实际iOS开发中UITableView是使用最多,也是最重要的一个控件,如果你不会用它,那别说什么大神了,菜鸟都不如. 其实关于UItableView事非常简单的,实 ...

  5. Fedora 20 安装后的一些事情

    1.关闭selinux 可以在软件中,找到selinux管理工具:system-config-selinux.py 2.安装源 可以通过# ls -l /etc/yum.repos.d 查看现有的安装 ...

  6. 第1章 游戏之乐——NIM(3)两堆石头的游戏

    NIM(3)两堆石头的游戏 1. 问题描述 假设有两堆石头,有两个玩家会根据如下的规则轮流取石头:每人每次可以从两堆石头中各取出数量相等的石头,或者仅从一堆石头中取出任意数量的石头:最后把剩下的石头一 ...

  7. 高级I/O之存储映射I/O

    存储映射I/O(Memory-mapped I/O)使一个磁盘文件与存储空间中的一个缓冲区相映射.于是当从缓冲区中取数据,就相当于读文件中的相应字节.与此类似,将数据存入缓冲区,则相应字节就自动地写入 ...

  8. 已知json类型根据类型封装集合

    1编写帮助类根绝url得到json public static string Post(string url) { string strURL = url; //创建一个HTTP请求 HttpWebR ...

  9. 基础知识 - Rabin-Karp 算法

    Rabin-Karp 算法(字符串快速查找) Go 语言的 strings 包(strings.go)中用到了 Rabin-Karp 算法.Rabin-Karp 算法是基于这样的思路:即把字符串看作是 ...

  10. 图片onerror(转)

    <script type="text/javascript"> <!– function nofind(){ var img=event.srcElement; ...