LeetCode OJ-- String to Integer (atoi) **
https://oj.leetcode.com/problems/string-to-integer-atoi/
细节题,把一个字符串转换成整数
class Solution {
public:
int atoi(const char *str) {
if(str == NULL)
return ;
// remove all spaces
int i = ;
while(str[i] != '/0' && str[i] == ' ')
{
i++;
}
// only contain spaces
if(str[i] == '/0')
return ;
// 判断符号
bool isNegative = false;
if(str[i] == '-')
{
i++;
isNegative = true;
}
else if(str[i] == '+')
i++;
else
{
// not num
if(str[i] < '' || str[i] > '')
return ;
}
double ans = ;
while(str[i] != '/0' && str[i]>='' && str[i] <='')
{
ans = ans*;
ans += str[i] - '';
// 如果是正数
if(isNegative == false && ans >= INT_MAX)
{
ans = INT_MAX;
break;
}
else if(isNegative && ans >= )
{
ans = INT_MIN*(-);
break;
}
i++;
}
if(isNegative)
ans = ans *(-);
return ans;
}
};
LeetCode OJ-- String to Integer (atoi) **的更多相关文章
- LeetCode OJ String to Integer (atoi) 字符串转数字
#include <iostream> #include <assert.h> using namespace std; int ato(const char *str) { ...
- 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】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. ...
- [LeetCode] 8. String to Integer (atoi) 字符串转为整数
Implement atoi which converts a string to an integer. The function first discards as many whitespace ...
随机推荐
- IntelliJ IDEA使用记录
一.快捷键 1. 生成main方法 在编写代码的时候直接输入psv就会看到一个psvm的提示,此时点击tab键一个main方法就写好了. psvm 也就是public static void main ...
- Tomcat中解决sql server连接失败--- java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver
php连接mysql数据库很容易,他俩真是黄金搭档.最近转战java连接微软sqlServer,步骤稍微复杂一点,但也不是太难,中途遇到了一点小问题,最后在csdn论坛里找到了答案http://bbs ...
- 从Windows 2012标准版升级到数据中心版,标准评价版本升级到标准体验版本并激活
对于Windows 7.Windows 8操作系统,可以在图形界面中通过输入序列号,从低版本直接升级到高的版本,例如从Windows 7家庭版升级到专业版或旗舰版.而对于Windows Server ...
- 稍览了一下CommonJS
CommonJS是服务器端模块的规范,现在炒得很火的Node.js采用了这个规范. 根据CommonJS规范,一个单独的文件就是一个模块.加载模块使用require方法,该方法读取一个文件并执行,最后 ...
- 【EF学习笔记09】----------使用 EntityState 枚举标记实体状态,实现增删改查
讲解之前,先来看一下我们的数据库结构:班级表 学生表 如上图,实体状态由EntityState枚举定义:Detached(未跟踪).Unchanged(未改变).Added(已添加).Deleted( ...
- webform repeater控件
Repeater: HeaderTemplate - 在加载开始执行一遍 ItemTemplate - 有多少条数据,执行多少遍 FooterTemplate - 在加载最后执行一遍 Alternat ...
- linux 错误处理
linux程序设计中,有许多系统调用和函数会因为各种原因而失败.在失败时设置外部变量errno的值来指明失败原因.程序必须在函数报告出错之后立即检查errno变量,因为它可能被下一个函数调用所覆盖(外 ...
- 深入理解js——隐式原型
每个函数都有一个prototye(原型),而每个对象都有一个_proto_,可成为隐式原型. _proto_是一个隐藏的属性,javascript不希望开发者用到这个属性值,有的低版本浏览器甚至不支持 ...
- IntelliJ IDEA13.1.3+Scala2.11.1环境搭建
fesh个人实践,欢迎经验交流!本文Blog地址:http://www.cnblogs.com/fesh/p/3805611.html Intellij IDEA的使用性非常好,是一个非常优秀的IDE ...
- .net 更新数据 ado.net parameter
UPDATE yborder_ordernotes SET recoder400= @FileAddress, havefile400 = 1 WHERE id = @OrderID Maticsof ...