题目描述:

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 Gym 100500C C. ICPC Giveaways 排序

    Problem C. ICPC GiveawaysTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/1005 ...

  2. Meteor 加入账户系统

    Meteor 加入账户系统 我们给meteor加入一个账户系统 导入包 meteor add ian:accounts-ui-bootstrap-3 meteor add accounts-passw ...

  3. JavaScript与Flash的通信

    当Flash置于HTML容器中时,经常会遇到AS与JS的通信问题,例如:JS能否调用AS中的变量.方法,AS能否调用JS中的变量.方法等等.答案是肯定的.随着技术的不断发展,解决方案也是多种多样的. ...

  4. Android APK方式换肤实现原理

    现在很多APP都有换肤的功能,例如微博,QQ等应用.这些应用的换肤原理是什么? 在用微博的时候,不难发现,当你要换肤时,先下载并安装一个皮肤apk,然后选择这个皮肤,就可以了. 这种方式就是把皮肤打包 ...

  5. android144 360 快捷方式

    package com.example; import android.net.Uri; import android.os.Bundle; import android.app.Activity; ...

  6. iOS UIButton加在window上点击无效果问题

    UIButton加在window上,点击没有效果,找了很久,原来是没有加上这名:[self.window makeKeyAndVisible]; self.window = [[UIWindow al ...

  7. 类型查找器 ITypeFinder

    NopCommerce为了支持插件功能,以及支持一些自动注册的功能.系统提供了类型查找器.ITypeFinder以及实现类就是提供此功能.通过类型查找器可以查找本程序域中的类,也可以查找整个bin目录 ...

  8. python 实现接口测试

    接口的类型有很多,但是我们经常遇见经常用的就get和post两种.这两种有什么区别呢?个人理解主要是表现在安全性方面. Python代码POST任意的HTTP数据以及使用Cookie的方法,有需要的朋 ...

  9. 小白日记26:kali渗透测试之提权(六)--收集敏感信息,隐藏痕迹

    提权后操作 提权之后,要收集目标系统的重要信息LINUX /etc/resolv.conf       #查看DNS配置,是否可以进行DNS篡改或劫持 /etc/passwd         #存放账 ...

  10. 深入研究Block用weakSelf、strongSelf、@weakify、@strongify解决循环引用(上)

    深入研究Block捕获外部变量和__block实现原理 前言 在上篇中,仔细分析了一下Block的实现原理以及__block捕获外部变量的原理.然而实际使用Block过程中,还是会遇到一些问题,比如R ...