(atoi)String to Integer 49

corner case的处理

?
?

整数一般考虑两点:一点是符号,另外一点是越界

?
?

首先去掉多余的空格字符

?
?

然后读符号,可能是正号,也可能是负号,也可能没有符号

?
?

然后按顺序读数字

?
?

结束条件有三:

1、异常字符出现——舍弃异常字符后的数据,保留前面的数作为结果;

2、数字越界——返回最接近的整数;

3、正常结束

?
?

长度为0,返回0

?
?

输入0,返回0,但有个finished,如果为true,则是输入0,如果为false,则是长度0,如果直接是异常字符,那么还是返回0,此时finished为false

?
?

?
?

package stringToInt49;

?
?

public class StringToInt49 {

static boolean finished = false;

public int atoi(String str) {

int length = str.length();

if (length == 0)

return 0;

int i = 0;

boolean minus = false;

if (str.charAt(0) == ‘-‘) {

minus = true;

i++;

} else if (str.charAt(0) == ‘+‘) {

i++;

}

long MIN_VALUE = Integer.MIN_VALUE;

long MAX_VALUE = Integer.MAX_VALUE;

long num = 0;

?
?

for (; i < length && !finished; i++) {

char c = str.charAt(i);

if (c >= ‘0‘ && c <= ‘9‘) {

num *= 10;

num += c - ‘0‘;

} else {

num=0;

break;

}

?
?

if (minus && 0 - num < MIN_VALUE) {

return Integer.MIN_VALUE;

}

if (!minus && num > MAX_VALUE) {

return Integer.MAX_VALUE;

}

}

if (i==length) {

finished = true;

}

return minus ? new Long(0 - num).intValue() : new Long(num).intValue();

}

public static void main(String[] args) {

StringToInt49 stringToInt49=new StringToInt49();

System.out.println(stringToInt49.atoi("123"));

}

}

?
?

时间: 2024-11-05 13:47:14

(atoi)String to Integer 49的相关文章

【leetcode】String to Integer (atoi)

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 f

leetCode 8. String to Integer (atoi) 字符串

8. 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 intende

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

题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 1 class Solution { 2 fun myAtoi(str: String): Int { 3 val maxInt = "2147483647" 4 val maxIntS = "+2147483647" 5 val minIntS = "-21474836

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函数需要满足以下特征 忽略字符串第一个非空格字符前的所

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,

【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

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

[算法]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. Analysis The following cases should be considere

Leetcode #8 String to Integer (atoi) (E)

[Problem] 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