LeetCode题解——String to Integer(atoi)
题目:
字符串转换为数字。
解法:
这道题的意思是要考虑到,如果有前置的空字符,则跳过;如果超出数字范围,则返回最大/最小整数;如果碰到第一个不能转换的字符,则返回。
代码:
class Solution {
public:
int atoi(const char *str) {
int sign = , result = ;
int len = strlen(str);
int i = ;
for( ; i < len && str[i] == ' '; ++i) //跳过前置的空字符
;
if(str[i] == '+') //如果有符号且为正
++i;
else if(str[i] == '-') //负数
{
sign = -;
++i;
}
for( ; i < len; ++i)
{
if(str[i] < '' || str[i] > '') //不能转换的字符
break;
if(result > INT_MAX/ || (result == INT_MAX/ && str[i] > INT_MAX% + '')) //即将超出int范围
return sign == - ? INT_MIN : INT_MAX;
result *= ;
result += str[i] - '';
}
return result * sign; //记得加上符号位
}
};
LeetCode题解——String to Integer(atoi)的更多相关文章
- Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)
Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...
- 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 ...
- 【leetcode】String to Integer (atoi)
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- [leetcode] 8. String to Integer (atoi) (Medium)
实现字符串转整形数字 遵循几个规则: 1. 函数首先丢弃尽可能多的空格字符,直到找到第一个非空格字符. 2. 此时取初始加号或减号. 3. 后面跟着尽可能多的数字,并将它们解释为一个数值. 4. 字符 ...
- Leetcode 8. String to Integer (atoi)(模拟题,水)
8. String to Integer (atoi) Medium Implement atoi which converts a string to an integer. The functio ...
- [LeetCode][Python]String to Integer (atoi)
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/string- ...
- LeetCode——8. String to Integer (atoi)
一.题目链接:https://leetcode.com/problems/string-to-integer-atoi/ 二.题目大意: 实现一个和C语言里atoi具有相同功能的函数,即能够把字符串转 ...
- 【LeetCode】String to Integer (atoi) 解题报告
这道题在LeetCode OJ上难道属于Easy.可是通过率却比較低,究其原因是须要考虑的情况比較低,非常少有人一遍过吧. [题目] Implement atoi to convert a strin ...
- LeetCode 8. String to Integer (atoi) (字符串到整数)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
随机推荐
- CF 353C Find Maximum #205 (Div. 2)
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; ]; ] ...
- POSIX semaphore: sem_open, sem_close, sem_post, sem_wait
http://www.cnblogs.com/BloodAndBone/archive/2011/01/18/1938552.html 一.Posix有名信号灯 1.posix有名信号灯函数 函数se ...
- 关于NGUI制作图集在低内存设备上的注意事项
正在写一个游戏.由于2D且比较简单.打算用NGUI全权搞定,对,游戏内容也用NGUI. 想的很好,做的很爽.PC上跑起来happy. 天杀的诺基亚出了个手机叫lumia520,可用内存512M.单个程 ...
- 格林治时间,也就是返回从 UTC 1970 年 1 月 1 日午夜开始经过的毫秒数。
格林治时间,也就是返回从 UTC 1970 年 1 月 1 日午夜开始经过的毫秒数. (* Delphi获取13位格林治时间实现方法, 与java中的java.lang.System.currentT ...
- Java API ——Collection集合类 & Iterator接口
对象数组举例: 学生类: package itcast01; /** * Created by gao on 15-12-9. */ public class Student { private St ...
- ConcurrentDictionary的ToDictionary
如果Value是引用,那么在使用Value.Clear()的时候.会清空Value的所有元素,但是不会改变Value的引用 private static void Main() { try { var ...
- Android开发之获取系统管理权限,即DevicePolicyManager和DeviceAdminReceiver的使用
参考:http://www.cnblogs.com/androidez/archive/2013/02/17/2915020.html 1.创建AdminReceiver,继承DeviceAdminR ...
- Codeforces 672
题目链接:http://codeforces.com/contest/672/problem A. Summer Camp(打表) 题意:123456789...一串字符串,问第n个是什么数字. 塞一 ...
- word引用错误
错误 4317 无法嵌入互操作类型“Microsoft.Office.Interop.Word.ApplicationClass”.请改用适用的接口. 类型“Microsoft.Office.Inte ...
- uva10820Send a Table
筛法. 首先使cnt[i]=sqr(n/i),这样cnt[i]就表示gcd(x,y)大于等于i的数对的个数,然后倒序枚举减去gcd大于i的个数就可以得到ans[i].最终得到ans[1]. 这个算法单 ...