String to Integer
Implement function atoi
to convert a string to an integer.
If no valid conversion could be performed, a zero value is returned.
If the correct value is out of the range of representable values, INT_MAX(2147483647) or INT_MIN (-2147483648) is returned.
"10" => 10
"-1" => -1
"123123123123123" => 2147483647
"1.0" => 1
- public class Solution {
- public int myAtoi(String str) {
- if (str == null || str.length() == )
- return ;
- str = str.trim();
- if (str.length() == && (str.equals("+") || str.equals("-") || str.equals(".") || str.equals(""))) {
- return ;
- }
- boolean isPositive = true;
- long current = ;
- for (int i = ; i < str.length(); i++) {
- if (i == && str.charAt(i) == '+') {
- continue;
- } else if (i == && str.charAt(i) == '-') {
- isPositive = false;
- } else if (str.charAt(i) >= '' && str.charAt(i) <= '') {
- current = current * + str.charAt(i) - '';
- if (isPositive && current > Integer.MAX_VALUE)
- return Integer.MAX_VALUE;
- if (!isPositive && -current < Integer.MIN_VALUE)
- return Integer.MIN_VALUE;
- } else {
- break;
- }
- }
- if (!isPositive) {
- current = -current;
- }
- return (int) current;
- }
- }
String to Integer的更多相关文章
- 【leetcode】String to Integer (atoi)
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- No.008 String to Integer (atoi)
8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...
- 【LeetCode】7 & 8 - Reverse Integer & String to Integer (atoi)
7 - Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Notic ...
- leetcode第八题 String to Integer (atoi) (java)
String to Integer (atoi) time=272ms accepted 需考虑各种可能出现的情况 public class Solution { public int atoi( ...
- leetcode day6 -- String to Integer (atoi) && Best Time to Buy and Sell Stock I II III
1. String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully con ...
- String to Integer (atoi) - 字符串转为整形,atoi 函数(Java )
String to Integer (atoi) Implement atoi to convert a string to an integer. [函数说明]atoi() 函数会扫描 str 字符 ...
- Kotlin实现LeetCode算法题之String to Integer (atoi)
题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 class ...
- String与Integer问题
今天分享一下关于最近面试的问题,临近春节,而我在茫茫人海中奔波,今天面试了来到了中关村科技园,挺气派的,之前也是在外面看看,今天就去了,心里有点激动,恰好,正好赶上了上班时,看见它们的努力,我感到再累 ...
- LeetCode--No.008 String to Integer (atoi)
8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...
- leetcode-algorithms-8 String to Integer (atoi)
leetcode-algorithms-8 String to Integer (atoi) Implement atoi which converts a string to an integer. ...
随机推荐
- office 软件常用备忘(删除线/冻结等)
Word: 1 大纲显示/navigation display: 视图/View --> Navigation Pane (left) 2 Track changes: 显示删除线 / 修改地方 ...
- Fedora和Ubuntu下安装OpenGL开发环境配置
Fedora下OpenGl开发环境配置 开发OpenGL工程需要3个库文件和对应的头文件: libglut.so,libGLU.so,libGL.so, gl.h ,glu.h, glut.h 这些库 ...
- poj 1845 数论综合
题意:求A^B的所有因数的和 mod 9901 sol:一开始毫无思路,因为很多定理都不知道-_-|| 1. 整数的唯一分解定理: 任意正整数都有且只有一种方式写出其素因子的乘积表达式. A=(p1^ ...
- 2014ACMICPC西安网赛1006
题意:给你一个骰子的初始状态和可以进行的四种操作,求从初始状态到目标状态的最少操作次数 题目本身很简单,bfs即可.但是因为骰子有六个面,搜索判重和记录状态比较麻烦.这时候就需要神器STL了. #in ...
- 洛谷P2014 TYVJ1051 选课
题目描述 在大学里每个学生,为了达到一定的学分,必须从很多课程里选择一些课程来学习,在课程里有些课程必须在某些课程之前学习,如高等数学总是在其它课程之前学习.现在有N门功课,每门课有个学分,每门课有一 ...
- hihocoder #1270 建造基地
传送门 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在遥远的未来,小Hi成为了地球联邦外空间联合开发工作组的一员,前往一颗新发现的星球开发当地的重金属资源. 为了能够 ...
- POJ 2240Arbitrage(Floyd)
E - Arbitrage Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submi ...
- html标签(一)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- xss跨站实例总结
跨站脚本攻击(Cross-site scripting,通常简称为XSS)发生在客户端,可被用于进行窃取隐私.钓鱼欺骗.偷取密码.传播恶意代码等攻击行为. 恶意的攻击者将对客户端有危害的代码放到服务器 ...
- python之BIF函数在列表中的应用
1 Python 3.3.4 (v3.3.4:7ff62415e426, Feb 10 2014, 18:13:51) [MSC v.1600 64 bit (AMD64)] on win32 2 T ...