LeetCode算法入门- String to Integer (atoi)-day7

String to Integer (atoi):

Implement atoi which converts a string to an integer.

The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.

The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.

If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.

If no valid conversion could be performed, a zero value is returned.

Example 1:

Input: “42”
Output: 42
Example 2:

Input: " -42"
Output: -42
Explanation: The first non-whitespace character is ‘-’, which is the minus sign.
Then take as many numerical digits as possible, which gets 42.
Example 3:

Input: “4193 with words”
Output: 4193
Explanation: Conversion stops at digit ‘3’ as the next character is not a numerical digit.
Example 4:

Input: “words and 987”
Output: 0
Explanation: The first non-whitespace character is ‘w’, which is not a numerical
digit or a +/- sign. Therefore no valid conversion could be performed.
Example 5:

Input: “-91283472332”
Output: -2147483648
Explanation: The number “-91283472332” is out of the range of a 32-bit signed integer.
Thefore INT_MIN (−231) is returned.

解法:
主要注意几个方面的内容:
这题主要就是考虑一下corner case。

越界问题?
正负号问题?
空格问题?
精度问题?

代码如下:

class Solution {public int myAtoi(String str) {//1. 先判断字符串是否为空或者长度为0if(str == null || str.length() < 1)return 0;//2. 调用String.trim()方法来去除空格str = str.trim();int i = 0;char flag = '+';//3. 判断字符串的正负,用于后面的判断if(i < str.length() && str.charAt(i) == '-'){flag = '-';i++;}else if(i < str.length() && str.charAt(i) == '+'){flag = '+';i++;}//4. 截取字符串中数字的部分,如何判断:str.charAt(i) >= '0' && str.charAt(i) <= '9'//记得result要用double类型来存储,不然可能会溢出double result = 0;while(i < str.length() && str.charAt(i) >= '0' && str.charAt(i) <= '9'){//每个字符每个字符取出来累加result = result * 10 + (str.charAt(i) - '0');i++;}//5. 利用前面的正负来给数组赋值if(flag == '-')result = -result;//6. 判断有没有超出最大值或者小于最小值if(result > Integer.MAX_VALUE)return Integer.MAX_VALUE;else if(result < Integer.MIN_VALUE)return Integer.MIN_VALUE;//最后记得将结果强制类型转换为int类型return (int)result;}
}

LeetCode算法入门- String to Integer (atoi)-day7相关推荐

  1. LeetCode算法入门- Roman to Integer Integer to Roman -day8

    LeetCode算法入门- Roman to Integer -day8 Roman to Integer: 题目描述: Roman numerals are represented by seven ...

  2. [LeetCode] NO. 8 String to Integer (atoi)

    [题目] Implement atoi to convert a string to an integer. [题目解析] 该题目比较常见,从LeetCode上看代码通过率却只有13.7%,于是编码提 ...

  3. Kotlin实现LeetCode算法题之String to Integer (atoi)

    题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 1 clas ...

  4. 【细节实现题】LeetCode 8. String to Integer (atoi)

    LeetCode 8. String to Integer (atoi) Solution1:我的答案 参考链接:http://www.cnblogs.com/grandyang/p/4125537. ...

  5. LeetCode 8. String to Integer (atoi)(字符串)

    LeetCode 8. String to Integer (atoi)(字符串) LeetCode 8 String to Integer atoi字符串 问题描述 解题思路 参考代码 By Sca ...

  6. LeetCode算法入门- Multiply Strings -day18

    LeetCode算法入门- Multiply Strings -day18 题目介绍 Given two non-negative integers num1 and num2 represented ...

  7. LeetCode算法入门- Compare Version Numbers -day14

    LeetCode算法入门- Compare Version Numbers -day14 题目描述: Compare two version numbers version1 and version2 ...

  8. LeetCode算法入门- Longest Valid Parentheses -day12

    LeetCode算法入门- Longest Valid Parentheses -day12 Given a string containing just the characters '(' and ...

  9. LeetCode算法入门- Reverse Integer-day6

    LeetCode算法入门- Reverse Integer-day6 Given a 32-bit signed integer, reverse digits of an integer. Exam ...

最新文章

  1. 电子学会 软件编程(图形化)一级训练营
  2. 收藏 | 深度学习在计算机视觉领域的应用总结
  3. wireshark tcp data中文_wireshark流量分析入门
  4. cocos2d-x 3.2 listview scorllview 等容器在小米华为等部分手机显示泛白解决
  5. 基于量子粒子群算法实现天线阵列优化
  6. 39 | 案例篇:怎么缓解 DDoS 攻击带来的性能下降问题?
  7. 【SPOJ5971】LCMSUM
  8. Linux和windows网络配置
  9. DBC 2000 安装
  10. Matlab里DTW算法和图像
  11. axure原型素材模板-手机端蓝色科幻科技动态酷炫游戏大数据手机H5页面模板素材聊天
  12. 三元运算符语法格式php,php中三元运算符用法详解
  13. 我的世界里 有你还不知道的秘密 边走边学习 且行且珍惜吧
  14. Java项目(前端vue后台java微服务)在线考试系统(java+vue+springboot+mysql+maven)
  15. 数据库 之 round函数
  16. 如何用代码画出一幅好看的画
  17. 如何查看windows 10 神州网信政府版的版本信息
  18. 批量把大量图片名称批量导入记事本或excel表格里(生成图片名目录)
  19. matlab时间转为数字,如何将日期数组(格式'mm/dd/yy HH:MM:SS')转换为数字?
  20. python 递归函数返回值

热门文章

  1. MySQL 索引的面试题总结
  2. 五、华为鸿蒙HarmonyOS应用开发之Java开发模式下的同一个 Page 里实现页面跳转时无参(有参)传递、回值详解
  3. 安卓逆向之基于Xposed-ZjDroid脱壳 逆向分析(脱壳)
  4. java 存储png文件_vue图片上传及java存储图片(亲测可用)
  5. boost asio io_context 没任务不退出
  6. mysql配置两个猪数据库_Linux下安装启动多个Mysql
  7. ppt讲解中的过渡_PPT黑科技,只用一张图做出3D动画
  8. shrio 登陆后 还是失效_在 iPhone 上取消订阅后,应用或内容会立即失效吗?
  9. 手机联系人头像包_一组抖音上很火的表情包,这里都有,一起来可可爱爱吧
  10. 上位机与1200组态步骤_西门子1200的HSC的应用实例!