题目

       题目很简单,就是写一个函数把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;}}}
代码下载:https://github.com/jimenbian/GarvinLeetCode

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

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

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

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

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

  1. 【LeetCode从零单排】No.7 Reverse Integer

    前话       今天开始励志刷一下leetcode上面的题目(还好这个网站没被TG和谐).从easy的开始,数一下差不多有40道,争取两个月搞定. 题目        没想到做的第一道题目,虽然看似 ...

  2. 【LeetCode从零单排】No198.House Robber No91.Decode Ways139 word break(动态规划典型应用)

    1.题目 一道典型的Dynamic Programming的题目. You are a professional robber planning to rob houses along a stree ...

  3. 【LeetCode从零单排】No70.ClimbingStairs

    题目 爬楼梯问题,这是一道很有趣的问题.首先看题目: You are climbing a stair case. It takes n steps to reach to the top. Each ...

  4. 【LeetCode从零单排】No22.Generate Parentheses

    题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...

  5. 【LeetCode从零单排】No 3 Longest Substring Without Repeating Characters

    题目 Given a string, find the length of the longest substring without repeating characters. For exampl ...

  6. 【LeetCode从零单排】No189 .Rotate Array

    题目 Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the arr ...

  7. 【LeetCode从零单排】No67.AddBinary

    题目 Given two binary strings, return their sum (also a binary string). For example, a = "11" ...

  8. 【LeetCode从零单排】No58.Length of Last Word

    题目 Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return th ...

  9. 【LeetCode从零单排】No38.CountAndSay

    题目 The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111 ...

最新文章

  1. saltstack pkg模块用法
  2. js取整、四舍五入等数学函数
  3. 100的阶乘c语言代码,求10000的阶乘(c语言代码实现)
  4. Cordova(PhoneGap) 环境搭建与基础
  5. LeetCode 1863. 找出所有子集的异或总和再求和(DFS)
  6. 改ip地址会有什么影响_led显示屏会带来什么负面影响
  7. 一文带你 GNN 从入门到起飞,做一个饭盆最稳 GNN 饭人!
  8. Struts2教程3:struts.xml常用配置解析
  9. 永久的CheckBox(单选,全选/反选)!
  10. JavaSE——Java8之Stream流
  11. Cadence PSpice 模型3:从官网或者技术支持得到的PSpice模型与Capture库关联方法图文教程
  12. 浓缩大学生活的顶级精华
  13. 我的世界服务器哪个有自动铺路,我的世界自动铺路指令是什么
  14. 挖掘数百万参与的IMVU用户
  15. 服务器端查看图片库 eog display Xforwarding
  16. C# 自定义动态九宫格键盘,简单实用
  17. 浙江大学软件学院人工智能保研面经2021
  18. 百趣代谢组学分享:从SWATH到MRM:一种新型高覆盖度靶向代谢组学技术
  19. 怎样才算得上是一名优秀的软件测试工程师呢?
  20. DIY TCP/IP IP模块和ICMP模块的实现2

热门文章

  1. PAT甲级1090 Highest Price in Supply Chain:[C++题解]树、结点到根结点的距离、记忆化搜索、树形dp
  2. 数据结构_栈和队列的区别
  3. 台湾国立大学郭彦甫Matlab教程笔记(21)linear equations(高斯消去法和追赶法)
  4. LayoutInflater中调用系统服务
  5. java is number_数据类型----Number
  6. mysql主从复制自增_关于mysql主从复制自增长列
  7. 九江学院计算机主任黄冬久,陈春生副校长到实验中间调研引导工作
  8. android 关闭多点触控_Cocos Creator关闭多点触摸的问题
  9. 小乌龟 拉取项目_从有个叫“乌龟石”的地方讲起
  10. jenkins配置python脚本参数_jenkins配置python脚本定时任务过程图解