【LeetCode从零单排】No.8 String to Integer (丧心病狂的一道题)

简介: 题目       题目很简单,就是写一个函数把string转换成int,但是通过率只有可怜的11%,难点是要考虑所有情况,特别是int溢出边界,反正我是写了2个小时还没解决,先放到这,有空接着搞,现在应该还有最后一个bug。Implement atoi to convert a string to an integer.Hint: Carefully consider all pos

题目

       题目很简单,就是写一个函数把string转换成int,但是通过率只有可怜的11%,难点是要考虑所有情况,特别是int溢出边界,反正我是写了2个小时还没解决,先放到这,有空接着搞,现在应该还有最后一个bug。

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.


代码

public class Solution {
    public int atoi(String str) {
  		String num_str="";
        char[] str_char=str.toCharArray();
        char[] sympo="-+".toCharArray();
        boolean abs=true;
        for(int i=0;i<str.length();i++){
        	 if(str_char[i]==sympo[0] ||str_char[i]==sympo[1]){
        		 if(!Character.isDigit(str_char[i+1])){
        			 return 0;
        		 }
        		 if(str_char[i]==sympo[0]){
        			 abs=false;
        		 }
        	 }
      	  if(Character.isDigit(str_char[i])){       		 
   	   	      num_str+=String.valueOf(str_char[i]);      	   	 
   	        }
      	  else{
      		  if(num_str.length()!=0){
      			  break;
      		  }
      	  }
//       	  if(Character.isDigit(str_char[i])){       		 
//       	   	 num_str+=String.valueOf(str_char[i]);      	   	 
//       	    }
       	 if(num_str!=""){
       	   if(Math.abs(Integer.parseInt(num_str))>=Integer.MAX_VALUE / 10){
  		      return Integer.MAX_VALUE;
  	          }
       	   }
       }
        if(num_str!=""){
        	 if(abs){
        		 return Integer.parseInt(num_str);
        		 }
        	 else{
        		 return -Integer.parseInt(num_str);
        	 }
	    }
        else{
        	   return 0;
        
  }
    
 }
    
}



/********************************

* 本文来自博客  “李博Garvin“

* 转载请标明出处:http://blog.csdn.net/buptgshengod

******************************************/


目录
相关文章
|
7月前
|
算法 C++
【LeetCode】【C++】string OJ必刷题
【LeetCode】【C++】string OJ必刷题
41 0
|
2月前
|
机器学习/深度学习 canal NoSQL
从C语言到C++_12(string相关OJ题)(leetcode力扣)
从C语言到C++_12(string相关OJ题)(leetcode力扣)
35 0
|
2月前
|
存储 编译器 Linux
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
|
8月前
|
Java
Leetcode 467. Unique Substrings in Wraparound String
大概翻译下题意,有个无限长的字符串s,是由无数个「abcdefghijklmnopqrstuvwxy」组成的。现在给你一个字符串p,求多少个p的非重复子串在s中出现了?
34 0
|
缓存 JSON NoSQL
Map<Integer,Value>放入缓存后取出来变成了Map<String,Value>
Map<Integer,Value>放入缓存后取出来变成了Map<String,Value>
184 0
|
JSON NoSQL Redis
关于Redis-存Long取Integer类型转换错误的问题;String对象被转义的问题
关于Redis-存Long取Integer类型转换错误的问题;String对象被转义的问题
440 0
|
算法 索引
【LeetCode】string 类的几道简单题
【LeetCode】string 类的几道简单题
【LeetCode】string 类的几道简单题
|
Java
Java数据类型中String、Integer、int相互间的转换
Java数据类型中String、Integer、int相互间的转换
170 0
|
存储 C语言 C++
Leetcode17. 电话号码的字母组合:递归树深度遍历(C++vector和string的小练习)
Leetcode17. 电话号码的字母组合:递归树深度遍历(C++vector和string的小练习)
|
存储 canal 算法
leetcode:43. 字符串相乘(附加一些C++string其他小练习)
leetcode:43. 字符串相乘(附加一些C++string其他小练习)