LeedCode OJ -- String to Integer (atoi)
题目意思就是自己实现一个atoi函数,也就是将字符串转换成int型。
关于INT_MAX和INT_MIN, 只是在<limits.h>文件中定义的宏..分别是int型可以表示的最大值和最小值
还有就是定义大整数常量的时候,会出现这种警告:warning: this decimal constant is unsigned only in ISO C90
c的标准写道:
The C90 rule that the default type of a decimal integer constant is either int, long, or
unsigned long, depending on which type is large enough to hold the value without overflow,
simplifies the use of constants. The choices in C99 are int, long and long long.
C89 added the suffixes U and u to specify unsigned numbers. C99 adds LL to specify long
long.
Unlike decimal constants, octal and hexadecimal constants too large to be ints are typed as
unsigned int if within range of that type, since it is more likely that they represent bit
patterns or masks, which are generally best treated as unsigned, rather than “real” numbers.
Little support was expressed for the old practice of permitting the digits 8 and 9 in an octal
constant, so it was dropped in C89.
A proposal to add binary constants was rejected due to lack of precedent and insufficient utility.
Despite a concern that a “lower-case-l” could be taken for the numeral one at the end of a
numeric literal, the C89 Committee rejected proposals to remove this usage, primarily on the
grounds of sanctioning existing practice.
解决方式:
1 在常数后面增加一个UL标识,或者ULL表示,如4294967295UL,这样就不会报警了
2 使用十六进制的数字,如0xFFFFFFFF
3 使用gcc -std=c99 用99标准来编译
附上代码:
class Solution {
public:
int atoi(const char *str) {
unsigned long long ans = ;
// is positive ?
int flag = ;
while (*str == ' ') str++;
if (*str == '+') {
str++;
} else if (*str == '-') {
flag = ;
str++;
}
while (*str != '\0') {
if ((*str) < '' || (*str) > '')
break;
ans = ans * + (*str) - '';
if (flag and ans > 2147483647ULL) {
return INT_MAX;
} else if (!flag and ans > 2147483648ULL) {
return INT_MIN;
}
str++;
}
if (!flag) {
ans = -ans;
}
return (int)ans;
}
};
LeedCode OJ -- String to Integer (atoi)的更多相关文章
- 【LeedCode】String to integer(atoi)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- LeetCode OJ String to Integer (atoi) 字符串转数字
#include <iostream> #include <assert.h> using namespace std; int ato(const char *str) { ...
- 【leetcode】String to Integer (atoi)
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- No.008 String to Integer (atoi)
8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...
- leetcode第八题 String to Integer (atoi) (java)
String to Integer (atoi) time=272ms accepted 需考虑各种可能出现的情况 public class Solution { public int atoi( ...
- leetcode day6 -- String to Integer (atoi) && Best Time to Buy and Sell Stock I II III
1. String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully con ...
- String to Integer (atoi) - 字符串转为整形,atoi 函数(Java )
String to Integer (atoi) Implement atoi to convert a string to an integer. [函数说明]atoi() 函数会扫描 str 字符 ...
- Kotlin实现LeetCode算法题之String to Integer (atoi)
题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 class ...
- LeetCode--No.008 String to Integer (atoi)
8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...
随机推荐
- JSP - (Java Server Pages) - Java服务器界面
JSP简介: 在HTML中嵌入Java脚本代码,由应用服务器中的JSP引擎来编译和执行嵌入的Java脚本代码,然后将生成的整个页面信息返回给客户端: 一个JSP页面包含:静态内容(HTML静态文本), ...
- Vue. 之 Element dialog 拖拽
Vue. 之 Element dialog 拖拽 默认情况下,在使用Element的Dialog模块时,弹出框是不能移动的,且 一旦点击遮罩层区域,弹框就会消失. 解决方案: 1 在 utils 中新 ...
- 禅道Mysql默认密码修改
1.安装禅道之后进入MySql数据库时提示密码错误:(禅道数据库默认用户名和密码admin,密码无) 2.此时需要修改MySql用户名和密码才可进入禅道数据库: 3.在Linux中执行命令 /op ...
- Nonsense Time
Nonsense Time 时间限制: 10 Sec 内存限制: 128 MB 题目描述 You a given a permutation p1,p2,…,pn of size n. Initia ...
- openCV图像合成
#include <iostream> #include <opencv2/opencv.hpp> #include <opencv2/highgui/highgui.h ...
- Javascript-简单的欢迎cookie
0<!DOCT0000YPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml&quo ...
- bzoj 1800 [Ahoi2009]fly 飞行棋——模拟
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1800 原来只想到一个弧是一条边. 然后发现不是.差点不会做.经Zinn提醒,不用枚举那条边由 ...
- TZ_05_Spring_转账事务基于xml的开发
事务:通过接口的动态代理加强AccountService 实现转账的事务 ApplicationContext.xml <?xml version="1.0" encodin ...
- cookie记录
登录页面引用: <script src="/jquery.cookie.js"></script> 登录页面jq: var telphone = $('[n ...
- “Bootstrap做的响应式菜单在iPhone上点击不了二级菜单“的解决办法!
只需把把点击的a(被点击的)变成button即可.