博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
String to Integer (atoi)
阅读量:4972 次
发布时间:2019-06-12

本文共 2680 字,大约阅读时间需要 8 分钟。

描述

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.
分析
细节题。注意几个测试用例:
1. 不规则输入,但是有效,”-3924x8fc”,” + 413”,

2. 无效格式,” ++c”, ” ++1”

3. 溢出数据,”2147483648”

代码

1 public class StringtoInteger { 2     static int INT_MAX = 2147483647; 3     static int INT_MIN = -2147483648; 4  5     public static void main(String[] args) { 6         // TODO Auto-generated method stub 7         String str = "-39x24x8fc"; 8         System.out.println(atoi(str)); 9     }10     // LeetCode, String to Integer (atoi)11     // 时间复杂度 O(n),空间复杂度 O(1)12 13     public static int atoi(String str) {14         int num = 0;15         int sign = 1;16         int n = str.length();17         int i = 0;18         while (str.charAt(i) == ' ' && i < n)19             i++; // 去空格20         if (str.charAt(i) == '+')21             i++; // 判符号22         if (str.charAt(i) == '-') { // 判符号-23             sign = -1; // sign符号24             i++;25         }26         for (; i < n; i++) { //27             if (str.charAt(i) < '0' || str.charAt(i) > '9')28                 // break; //跳出本循环 得到部分数字29                 continue; // 跳出本次循环,不运行循环内的下面程序,然后跳到下次循环。得到全部数字30             if (num > INT_MAX / 10 || 31                 (num == INT_MAX / 10 && (str.charAt(i) - '0') > INT_MAX % 10)) { // 判超限32                 return sign == -1 ? INT_MIN : INT_MAX;33             }34             num = num * 10 + str.charAt(i) - '0';35         }36         return num * sign;37     }38 39 }

 

转载于:https://www.cnblogs.com/ncznx/p/9176886.html

你可能感兴趣的文章
随机迷宫算
查看>>
JSON
查看>>
2016.8.23 项目总结
查看>>
RBAC
查看>>
王爽-汇编语言-综合研究五-函数接收不定量参数
查看>>
[HAOI2015][bzoj 4033]树上染色(树dp+复杂度分析)
查看>>
C++ Boost在VS2015中的使用
查看>>
leetcode 12 -> Integer to Roman
查看>>
Ubuntu 14.04 安装Docker
查看>>
如果已经建立了连接,但是客户端突然出现故障了怎么办?
查看>>
洛谷 1414 数论 分解因数 水题
查看>>
ASP.NET MVC中controller和view相互传值的方式
查看>>
set集合
查看>>
SSH
查看>>
IOS 网络浅析-(六 网络图片获取之三方SDWebImage)
查看>>
Zookeeper 安装
查看>>
python self
查看>>
redis 重启
查看>>
EBS R12 查询EBS用户相关SQL
查看>>
推荐阅读
查看>>