LeetCode OJ 之 String to Integer (atoi字符串转数字)
来源:互联网 发布:在淘宝网上买车可靠吗 编辑:程序博客网 时间:2024/05/07 03:05
题目:
出处:点击打开链接
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.
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.
#include<iostream>using namespace std;//最大最小值在limits.h中已经定义//#define INT_MIN (-2147483647 - 1) /* minimum (signed) int value *///#define INT_MAX 2147483647 /* maximum (signed) int value */int atoi_x(const char *str){int num = 0;int sign = 1;//记录数字符号int sign_num=0;//记录符号出现的次数const char *p = str;//需定义为const,否则类型不匹配while (*p == ' ') p++;if (*p == '+') {sign_num++;p++;}if (*p == '-') {sign_num++;sign = -1;p++;}while(*p){//如果有效首字母非数字,则直接退出循环if (*p < '0' || *p > '9'||sign_num>1)break;//如果超出范围,则返回最大值或者最小值if (num > INT_MAX / 10 || (num == INT_MAX / 10 &&(*p - '0') > INT_MAX % 10)) {return sign == -1 ? INT_MIN : INT_MAX;}num = num * 10 + ( *p - '0' );p++;}return num * sign;}int main(){char str1[]={'+','1','2','3','4','5'};//字符数组char str2[]="-2147483648";char str3[]="2147483651";//溢出数据char str4[]="+-12345";//无效格式char str5[]=" -12345";char str6[]="-3924x8fc";//不规则输入,但是有效cout<<atoi_x(str1)<<endl;cout<<atoi_x(str2)<<endl;cout<<atoi_x(str3)<<endl;cout<<atoi_x(str4)<<endl;cout<<atoi_x(str5)<<endl;cout<<atoi_x(str6)<<endl;}
更新C++:
class Solution {public: int myAtoi(string str) { int len = str.size(); long long result = 0; if(len == 0) return 0; int i = 0; bool minus = false; while(i < len && str[i] == ' ') i++; if(i < len && str[i] == '+') i++; else if(i < len && str[i] == '-') { i++; minus = true; } while(i < len ) { if(str[i] > '9' || str[i] < '0') break; int flag = minus ? -1 : 1; result = result * 10 + flag * (str[i] - '0'); if(!minus && result > INT_MAX) return INT_MAX; if(minus && result < INT_MIN) return INT_MIN; i++; } return (int)result; }};
java:
public class Solution { public int myAtoi(String str) { int len = str.length(); int flag = 1; int i = 0 ; long result = 0; while(i < len && str.charAt(i) == ' ') i++; if(i < len && str.charAt(i) == '+') i++; else if(i < len && str.charAt(i) == '-') { i++; flag = -1; } while(i < len) { if(str.charAt(i) < '0' || str.charAt(i) > '9') break; result = result * 10 + flag * (str.charAt(i) - '0'); if(result > Integer.MAX_VALUE) return Integer.MAX_VALUE; if(result < Integer.MIN_VALUE) return Integer.MIN_VALUE; i++; } return (int)result; }}
- LeetCode OJ 之 String to Integer (atoi字符串转数字)
- LeetCode 8. String to Integer (atoi)(字符串转数字)
- Leetcode#8. String to Integer (atoi)(字符串转数字)
- LeetCode--String to Integer (atoi)字符串转数字
- Leetccode String to Integer (atoi) 字符串转数字
- LeetCode #8 String to Integer (atoi) 字符串转数字 解题小节
- LeetCode OJ:String to Integer (atoi)
- LeetCode OJ - String to Integer (atoi)
- [LeetCode OJ][008]String to Integer (atoi)
- LeetCode OJ String to Integer (atoi)
- <LeetCode OJ> 8. String to Integer (atoi)
- LeetCode OJ - String to Integer (atoi)
- LeetCode OJ 8.String to Integer (atoi)
- leetCode OJ 8. String to Integer (atoi)
- LeetCode 8. String to Integer (atoi)(字符串)
- leetcode - 字符串转换成数字(String to Integer)atoi
- leetCode 之 String to Integer (atoi)
- leetcode 之 String to Integer (atoi)
- Windows系统版本判定那些事儿
- 到底谁是傻瓜
- Spring学习【Spring概述】
- 程序员面试题(二)
- Foundation中的NSDate
- LeetCode OJ 之 String to Integer (atoi字符串转数字)
- D3DXMatrixPerspectiveFovLH,D3DXMatrixIdentity,D3DXMatrixOrthoLH
- memset
- 50道Java线程面试题
- ASP.NET动态网站开发培训-15.DataList控件(二)
- 使用CImage类将RGB图像转化为灰度图像
- ubuntu安装和查看已安装
- 模拟赛 藏宝图(时间限制:2s,空间限制:256MB)
- Java IdentityHashMap重复值的输出问题