算法系列——String to Integer (atoi)
来源:互联网 发布:北京大学网络教育官网 编辑:程序博客网 时间:2024/06/06 17:25
题目描述
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 myAtoi(String str) { int head=0; long sum=0; int flag=1; if(str==null || str=="") return 0; while(str.charAt(head)==' ') head++; if(str.charAt(head)=='-'){ flag=-1; head++; } else if(str.charAt(head)=='+'){ head++; } while(str.charAt(head)>='0'&&str.charAt(head)<='9'){ sum=sum*10+str.charAt(head)-'0'; if(sum*flag>Integer.MAX_VALUE) return Integer.MAX_VALUE; if(sum*flag<Integer.MIN_VALUE) return Integer.MIN_VALUE; head++; } return (int)sum*flag; }}
阅读全文
0 0
- 算法系列——String to Integer (atoi)
- LeetCode算法题——String to Integer (atoi)
- String to Integer (atoi)算法
- String——string-to-integer-atoi
- LeetCode——String to Integer (atoi)
- LeetCode——String to Integer (atoi)
- LeetCode8——String to Integer (atoi)
- String to Integer (atoi) —— Leetcode
- leetcode——String to Integer (atoi)
- <8>——String to Integer (atoi)
- LeetCode—8. String to Integer (atoi)
- 【leetcode系列】String to Integer (atoi)
- 字符串系列---string-to-integer-atoi
- 算法训练:String to Integer (atoi)
- LeetCode算法题目:String to Integer (atoi)
- String to Integer (atoi)(算法分析week6)
- <leetcode系列> String to Integer (atoi) 以及atoi源码实现
- String to Integer (atoi)
- Git在tortoiseGit以及eclipse的使用方法
- 实习第二天
- ArcGIS中的三维数据
- http://blog.csdn.net/wlmnzf/article/details/72855610?utm_source=itdadao
- 解决:MySQL Load Data数据丢失问题
- 算法系列——String to Integer (atoi)
- Java基本数据类型占用字节和范围
- 从涂鸦到发布 —— 理解API的设计过程
- SLAM入门资料汇总
- Linux互斥锁的使用
- oracle组函数和分组概要记录
- Statement和PreparedStatement之间的区别
- MATLAB命令
- Jsp与Servlet的关系以及Jsp与Html的区别