【leetcode】8. String to Integer (atoi)
题目描述:
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.
解题思路:
这道题简直让人抓狂!试了许多次都没有通过,原因是要考虑多种情况。主要考虑的因数有:
- 要考虑前几个或全部字符都是空白字符的情况;
- 要考虑第一个字符是+,-,还是0-9的字母,或是其他字符;
- 要考虑有没有值得溢出,尤其是最小值溢出;
- 若中间有非数字字符,要返回之前的数字,如“123a34”要返回123;
- 若以若干个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)的更多相关文章
- 【LeetCode】8. String to Integer (atoi) 字符串转换整数
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字符串转整数,atoi,题解,Leetcode, 力扣,P ...
- 【LeetCode】8. String to Integer (atoi) 字符串转整数
题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...
- 【LeetCode】008. String to Integer (atoi)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- 【一天一道LeetCode】#8. String to Integer (atoi)
一天一道LeetCode系列 (一)题目 Implement atoi to convert a string to an integer. Hint: Carefully consider all ...
- 【LeetCode】字符串 string(共112题)
[3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...
- 《LeetBook》leetcode题解(8): String to Integer (atoi) [E]——正负号处理
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 【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❤python】 8. String to Integer (atoi)
#-*- coding: UTF-8 -*-#需要考虑多种情况#以下几种是可以返回的数值#1.以0开头的字符串,如01201215#2.以正负号开头的字符串,如'+121215':'-1215489' ...
- 【LeetCode】7、Reverse Integer(整数反转)
题目等级:Easy 题目描述: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 O ...
随机推荐
- BZOJ 2588: Spoj 10628. Count on a tree 树上跑主席树
2588: Spoj 10628. Count on a tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/J ...
- 使用C# 实现文件锁
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 使用C#: 自动切换鼠标的左右手习惯
不知道我得的是鼠标手,还是肩周炎. 长时间右手(或者左手)使用鼠标的话,那只胳膊便会不自在. 于是便有了切换鼠标主次要键的需求. [控制面板->鼠标]有更改它的设置,可点来点去让我觉得不够方便. ...
- WPF UI布局之概述
在线演示:http://v.youku.com/v_show/id_XNzA5NDk2Mjcy.html 清晰版视频+代码下载:http://115.com/lb/5lbeer0m9lad 一.简单介 ...
- Jquery-Ajax常用总结
1.方式一:访问.aspx 客户端: function Del(Id) { if (confirm("确认要删除?")) { $.ajax({ type: "Post&q ...
- 查看修改swap空间大小
1 查看swap 空间大小(总计): # free -m 默认单位为k, -m 单位为M total used fr ...
- LINUX 内核导论
http://blog.csdn.net/ljy1988123/article/category/1490573/2
- Ruby on Rails Tutorial 第六章 用户模型
1.用户模型(1)数据库迁移Rails默认使用关系数据库存储数据,数据库中的表有数据行组成,每一行都有相应的列,对应数据属性.把列名命名为相应的名字后,ActiveRecord会自动把他们识别为用户对 ...
- HOWTO install Oracle 11g on Ubuntu Linux 12.04 (Precise Pangolin) 64bits
安装了Ubuntu 12.04 64bit, 想在上面安装Oracle 11gr2,网上找了好多文档都没成功,最后完全参考了MordicusEtCubitus的文章. 成功安装的关键点:install ...
- win7引导项顺序
转载:http://jingyan.baidu.com/article/72ee561aa1d123e16138df81.html 问题描述: 个人在宿舍使用的比较多的是Window 7,而它的启动项 ...