【LeetCode】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 intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.

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 optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.

  The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.

  If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.

  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.

思路:

  该题目是说将string类型的字符串转换成整型数据,类似于C++库里的atoi函数,解决该题目的关键在于两个方面:

  (1)字符串格式的合法判断

  (2)转换结果的溢出判断

public class Solution {
    public int myAtoi(String str) {
        str=str.trim();
        char[] arr=str.toCharArray();
        if(arr.length==0){
            return 0;
        }

        int i=0;
        boolean symbol=true;
        if(arr[i]==‘-‘){
            symbol=false;
            i++;
        }else if(arr[i]==‘+‘){
            i++;
        }

        long MAX_VALUE=Integer.MAX_VALUE;
        long MIN_VALUE=Integer.MIN_VALUE;
        long num=0;
        for(int j=i;j<arr.length;j++){
            if(arr[j]>=‘0‘&&arr[j]<=‘9‘){
                num=num*10+arr[j]-‘0‘;
            }else{
                break;
            }

            if(!symbol&&(0-num<MIN_VALUE)){
                return Integer.MIN_VALUE;
            }else if(symbol&&(num>MAX_VALUE)){
                return Integer.MAX_VALUE;
            }
        }

        return symbol?new Long(num).intValue():new Long(0-num).intValue();
    }
}
时间: 2024-10-09 11:37:09

【LeetCode】8. String to Integer (atoi) 字符串转整数的相关文章

[LeetCode]41. 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

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

leetcode - String to Integer (atoi) 字符串转整数

Implement atoi to convert a string to an integer. 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. 题目:实现字符串转整数 注意事项:考虑好各种可能的输入(坑); public class Solution

[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 this problem to be spe

LeetCode OJ String to Integer (atoi) 字符串转数字

1 #include <iostream> 2 #include <assert.h> 3 using namespace std; 4 int ato(const char *str) { 5 int i=0,e=0,s=0; 6 int max=2147483647,min=-2147483648; 7 int f=1; 8 int tem[10]={0}; 9 unsigned int pan=0; 10 while(*str==' '){ //过滤掉连续空格 11 str+

LeetCode中String to Integer (atoi) 字符串转换成整数

int atoi(const char *str) { long long result = 0; while (*str==' ')str++; int flag=0; if (*str == '-' || *str == '+') { flag = (*str == '-') ? -1 : 1; str++; } while (*str!='\0') { int temp = *str - '0'; if (temp >= 0 && temp <= 9) { result

leetcode——String to Integer (atoi) 字符串转换为整型数(AC)

mnesia在频繁操作数据的过程可能会报错:** WARNING ** Mnesia is overloaded: {dump_log, write_threshold},可以看出,mnesia应该是过载了.这个警告在mnesia dump操作会发生这个问题,表类型为disc_only_copies .disc_copies都可能会发生. 如何重现这个问题,例子的场景是多个进程同时在不断地mnesia:dirty_write/2 mnesia过载分析 1.抛出警告是在mnesia 增加dump

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 008 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 b