Leet Code OJ 8. String to Integer (atoi) [Difficulty: Easy]

题目:

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.

翻译:

实现一个atoi函数来把字符串转换为整型变量。

分析:

这道题的AC率只有13.4%,主要是因为对特殊情况的处理上。具体有这么几种情况需要考虑:

1. 前面的空格

2. 除去前面的空格后,可以以“+、-、0”开头,需要做对应的处理

3. 除了起始处可以出现前2种情况提到的非数字字符,其他地方一旦出现,则忽略该字符以及其后的字符

4. 考虑边界,即是否超出Integer.MAX_VALUE,Integer.MIN_VALUE。下面的方案采用long作为临时存储,方便做边界的判断。但是还要考虑是否会超出long的最大值,所以笔者采用length长度做初步判断。

Java版代码(时间复杂度O(n)):

public class Solution {
    public int myAtoi(String str) {
        char[] charArr=str.toCharArray();
        Long result=0L;
        int startIndex=0;
        boolean flag=true;//正数
        int length=0;
        for(int i=0;i<charArr.length;i++){
            if(startIndex==i){
                if(charArr[i]==‘ ‘){
                    startIndex++;
                    continue;
                }
                if(charArr[i]==‘+‘||charArr[i]==‘0‘){
                    continue;
                }
                if(charArr[i]==‘-‘){
                    flag=false;
                    continue;
                }
            }
            if(charArr[i]>=‘0‘&&charArr[i]<=‘9‘){
                result=result*10+charArr[i]-‘0‘;
                length++;
                if(length>10){
                    break;
                }
            }else{
                break;
            }
        }
        if(flag){
            if(result>Integer.MAX_VALUE){
                return Integer.MAX_VALUE;
            }
        }else{
            result=-result;
            if(result<Integer.MIN_VALUE){
                return Integer.MIN_VALUE;
            }
        }
        return result.intValue();
    }
}
时间: 2024-08-08 05:38:37

Leet Code OJ 8. String to Integer (atoi) [Difficulty: Easy]的相关文章

Leet Code OJ 119. Pascal&#39;s Triangle II [Difficulty: Easy]

题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space? 翻译: 给定一个下标k,返回第k行的杨辉三角. 例如给定k=3,返回[1,3,3,1]. 提示:你可以优化你的算法,让它只使用O(k)的额

Leet Code OJ 168. Excel Sheet Column Title [Difficulty: Easy]

题目: Given a positive integer, return its corresponding column title as appear in an Excel sheet. For example: 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> AB 翻译: 给定一个正数,返回它类似Excle中对应的列标题. 分析: 关联问题:"Excel Sheet Column Number"

Leet Code OJ 58. Length of Last Word [Difficulty: Easy]

题目: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exist, return 0. Note: A word is defined as a character sequence consists of non-space

Leet Code OJ 328. Odd Even Linked List [Difficulty: Easy]

题目: Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes. You should try to do it in place. The program should run in O(1) space comp

Leet Code OJ 21. Merge Two Sorted Lists [Difficulty: Easy]

题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 翻译: 合并2个已经排序的链表,并且返回一个新的链表.这个新的链表应该由前面提到的2个链表的节点所组成. 分析: 注意头节点的处理,和链表结束(next为null)的处理.以下代码新增了一个头指针,来把头节点

Leet Code OJ 203. Remove Linked List Elements [Difficulty: Easy]

题目: Remove all elements from a linked list of integers that have value val. Example Given: 1 –> 2 –> 6 –> 3 –> 4 –> 5 –> 6, val = 6 Return: 1 –> 2 –> 3 –> 4 –> 5 翻译: 从一个整数链表中移除所有value为val的元素. 例如 给定: 1 –> 2 –> 6 –> 3

【Leet Code】String to Integer (atoi) ——常考类型题

String to Integer (atoi) Total Accepted: 15482 Total Submissions: 106043My Submissions 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 yours

LeetCode【8】. String to Integer (atoi) --java实现

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

Leet Code OJ 118. Pascal&#39;s Triangle [Difficulty: Easy]

题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 翻译: 给定一个数numRows,产生前numRows行的杨辉三角(即贾宪三角形.帕斯卡三角形). 分析: 除了每行首尾是1以外,其他元素均可由上行推出,本方案采用lastLine保存上行数