实现atoi

1. 去掉首位空格

2. 判断首位是否有正负号

3. 判断各位是否是0~9,有其他字符直接返回当前结果

 1 public class Solution {
 2     public int atoi(String str) {
 3         str = str.trim();
 4         int result = 0;
 5         boolean isPos = true;
 6
 7         for(int i=0; i<str.length(); i++) {
 8             char c = str.charAt(i);
 9             if(i==0 && (c == ‘+‘ || c == ‘-‘)) {
10                 isPos = c == ‘+‘ ? true : false;
11             } else if(c >= ‘0‘ && c <= ‘9‘) {
12                 if(result > (Integer.MAX_VALUE- (c-‘0‘))/10) {
13                     return isPos ? Integer.MAX_VALUE : Integer.MIN_VALUE;
14                 }
15                 result = result * 10 + c - ‘0‘;
16             } else {
17                 return isPos ? result : -result;
18             }
19         }
20
21         return isPos ? result : -result;
22     }
23 }
时间: 2024-08-08 01:26:22

实现atoi的相关文章

LeetCode:String to Integer (atoi)

1.题目名称 String to Integer (atoi) (字符串到数字的转换) 2.题目地址 https://leetcode.com/problems/string-to-integer-atoi/ 3.题目内容 英文:Implement atoi to convert a string to an integer. 中文:实现atoi函数,将输入的字符串(String类型)转换为整型数据(Integer类型) 提示:实现的atoi函数需要满足以下特征 忽略字符串第一个非空格字符前的所

String to Integer (atoi) leetcode

题目的意思是要将一个字符串转换成数字 这个题目的重点是要处理    各种各样的输入情况 在题目下面有一大段英文: Requirements for atoi: 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 opt

Leetcode 数 String to Integer (atoi)

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie String to Integer (atoi) Total Accepted: 9862 Total Submissions: 67880 Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge,

8. String to Integer (atoi)

https://leetcode.com/problems/string-to-integer-atoi/description/ 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 poss

【LeedCode】String to integer(atoi)

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 spe

【C语言】模拟实现atoi函数

atoi(表示 ascii to integer)是把字符串转换成整型数的一个函数. atoi()函数会扫描参数 nptr字符串,跳过前面的空白字符(例如空格,tab缩进等,可以通过isspace( )函数来检测),直到遇上数字或正负符号才开始做转换,而再遇到非数字或字符串结束时('\0')才结束转换,并将结果返回.如果 nptr不能转换成 int 或者 nptr为空字符串,那么将返回0 我们在模拟实现atoi函数时,要注意以下几点: 1.字符串之前的空白问题 2.正负号 3.字符串为空时 4.

LeetCode8 String to Integer (atoi)

题意: Implement atoi to convert a string to an integer.  (Easy) 分析: 就是注意各种特殊情况,边界情况的判断,见代码注释. 1 class Solution { 2 public: 3 int myAtoi(string str) { 4 int start = 0; 5 long long result = 0; 6 int flag = 0; 7 //开头多余空格的处理 8 while (str[start] == ' ') { 9

[leetcode]经典算法题- String to Integer (atoi)

题目描述: 把字符串转化为整数值 原文描述: 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

c++ 实现atoi()函数

1. 问题描写叙述 实现c++函数库中atoi()函数,要考虑到各种特殊情况: 空字符串. +和-号. 字符串前中后n个空格. 溢出. 非数字字符. 2. 解决方式 转换过程并不复杂.复杂的是要考虑到众多特殊情况. int myAtoi(string str) { if(str.length() == 0) return 0; //空串 bool isNeg = false; long re = 0; int i=0,cnt; for(;i<str.length(); i++) if(str[i

String to Integer (atoi)

1. Question 将字符串转换为整数,考虑各种输入情况: 空格处理:开头空格省略 有效数字:从第一个非空格字符开始的是+.-或数字,直到下一个非数字字符结束. 加号处理:开头加号省略 空串处理 溢出处理 无效数字处理 Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do no