LeetCode ---8. String to Integer (atoi)
1 public int myAtoi(String str) { 2 long value = 0; 3 char sign = '\0'; 4 //是否已经存在非零值,存在true,不存在false 5 boolean existNonzero = false; 6 boolean over = false; 7 char ch; 8 9 for(int i = 0; !over && i < str.length(); i++) { 10 ch = str.charAt(i); 11 if(ch == ' ' || ch == '.' || ch == '+' || ch == '-' || (ch <= '9' && ch >= '0')) { 12 switch(ch) { 13 case '+' : 14 case '-' : 15 if(existNonzero) { 16 over = true; 17 } else if(sign == '\0') { 18 sign = (ch == '-') ? '-' : '+'; 19 existNonzero = true; 20 } 21 break; 22 case ' ' : 23 over = existNonzero; 24 break; 25 case '0' : 26 if(existNonzero) { 27 value *= 10; 28 } 29 break; 30 default : 31 value = (value >= 0) ? (value * 10 + ch - 48) : (value * 10 - ch + 48); 32 existNonzero = true; 33 break; 34 } 35 } else break; 36 if(sign == '-' && value > 0) { 37 value = -value; 38 } 39 if(value > Integer.MAX_VALUE) { 40 return Integer.MAX_VALUE; 41 } 42 if(value < Integer.MIN_VALUE) { 43 return Integer.MIN_VALUE; 44 } 45 } 46 return (int) value; 47 }
转载于:https://www.cnblogs.com/zousanchuan/p/6869755.html
LeetCode ---8. String to Integer (atoi)相关推荐
- 【细节实现题】LeetCode 8. String to Integer (atoi)
LeetCode 8. String to Integer (atoi) Solution1:我的答案 参考链接:http://www.cnblogs.com/grandyang/p/4125537. ...
- LeetCode 8. String to Integer (atoi)(字符串)
LeetCode 8. String to Integer (atoi)(字符串) LeetCode 8 String to Integer atoi字符串 问题描述 解题思路 参考代码 By Sca ...
- leetcode 8. String to Integer (atoi)
也许是我没有理解清楚题意,为什么输入+-2的时候要输出0,而不是输出2呢. public class Solution {public int myAtoi(String str) {if(str = ...
- LeetCode算法入门- String to Integer (atoi)-day7
LeetCode算法入门- String to Integer (atoi)-day7 String to Integer (atoi): Implement atoi which converts ...
- Kotlin实现LeetCode算法题之String to Integer (atoi)
题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 1 clas ...
- [LeetCode] NO. 8 String to Integer (atoi)
[题目] Implement atoi to convert a string to an integer. [题目解析] 该题目比较常见,从LeetCode上看代码通过率却只有13.7%,于是编码提 ...
- String to Integer (atoi) leetcode java
题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...
- 008 String to Integer (atoi) [Leetcode]
题目内容: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ...
- 8. String to Integer (atoi)
题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...
- LeetCodeOJ. String to Integer (atoi)
试题请參见: https://oj.leetcode.com/problems/string-to-integer-atoi/ 题目概述 Implement atoi to convert a str ...
最新文章
- 杜拉拉的作者李可应北大就业指导中心之约写给大学生的一封信
- 论文翻译_论文翻译的注意事项有什么?
- 前端面试题(html篇)
- 学前教育试题库及答案_最新《学前教育学》专科-试题库及答案资料
- java asynccontext,超时后的JavaEE 6 AsyncContext行为
- textarea如何实现高度自适应?
- 那些公司用计算机仪表电缆,计算机及仪表用电缆技术
- vs如何显示arcgis 二次开发工具控件
- svg 贝塞尔曲线图解(记录)
- m1系统怎么重装,m1芯片怎么重装系统,苹果M1芯片重装系统,m1芯片重新安装mac
- python 特征选择卡方_4. 机器学习之特征选择-Python代码
- 分辨率_电视分辨率多少合适
- 20191218每日一句
- 数据挖掘常用的基本技术,主要有哪些?
- android迷你世界助手52,迷你世界助手盒子刷迷你币
- flash花屏 html,电脑中Firefox火狐浏览器出现卡顿、花屏、Flash崩溃等问题的解决方法...
- go,go语言编辑器安装,git安装,即vscode三个插件的安装
- python寻找完全平方数_279 完全平方数(bfs)
- 送学妹的生日礼物~ 制作一个超浪漫的告白3D相册(HTML+CSS+JavaScript)
- 代码坏味道 之 9 基本类型偏执 primitive obsession
热门文章
- 使用 Mac 的 Safari 收看 Netflix 时出现错误码「S7361-1253」如何解决?
- 如何在Mac上使用触控栏?
- 如何使用 Mac 的通知中心?
- 苹果Mac 默认浏览器如何更改?设置成自己习惯用的浏览器
- 升级macOS Big Sur 无法开机/死机怎么办?
- 62. KVOController详解
- 引发电气火灾火警的原因(转载)
- HDU 4946 Area of Mushroom(2014 Multi-University Training Contest 8)
- java导出excel超出65536条处理
- 再见收费的 XShell,我改用国产良心工具,完全免费!