LeetCode.atoi
请你来实现一个 atoi 函数,使其能将字符串转换成整数。
首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止。
当我们寻找到的第一个非空字符为正或者负号时,则将该符号与之后面尽可能多的连续数字组合起来,作为该整数的正负号;假如第一个非空字符是数字,则直接将其与之后连续的数字字符组合起来,形成整数。
该字符串除了有效的整数部分之后也可能会存在多余的字符,这些字符可以被忽略,它们对于函数不应该造成影响。
注意:假如该字符串中的第一个非空格字符不是一个有效整数字符、字符串为空或字符串仅包含空白字符时,则你的函数不需要进行转换。
在任何情况下,若函数不能进行有效的转换时,请返回 0。
说明:
假设我们的环境只能存储 32 位大小的有符号整数,那么其数值范围为 [−231, 231 − 1]。如果数值超过这个范围,qing返回 INT_MAX (231 − 1) 或 INT_MIN (−231) 。
示例 1:
输入: "42"
输出: 42
示例 2:
输入: " -42"
输出: -42
解释: 第一个非空白字符为 '-', 它是一个负号。
我们尽可能将负号与后面所有连续出现的数字组合起来,最后得到 -42 。
示例 3:
输入: "4193 with words"
输出: 4193
解释: 转换截止于数字 '3' ,因为它的下一个字符不为数字。
示例 4:
输入: "words and 987"
输出: 0
解释: 第一个非空字符是 'w', 但它不是数字或正、负号。
因此无法执行有效的转换。
示例 5:
输入: "-91283472332"
输出: -2147483648
解释: 数字 "-91283472332" 超过 32 位有符号整数范围。
因此返回 INT_MIN (−231) 。
int myAtoi(char* str) {
if(!str){
return ;
}
bool inte = true;
char *tmp;
tmp = str;
signed int num=;
signed int compare_num = ;
signed int tmp_add=;
while(*tmp != '\0'){
if(*tmp == ' ')
{ tmp++;
continue;
}
if( (*tmp >= '' && *tmp <= '') || *tmp == '+' || *tmp == '-')
{
if(*tmp == '-'){
inte = false;
tmp++;
break;
}
if(*tmp == '+'){
inte = true;
tmp++;
}
break;
}
else{
return ;
}
}
while(*tmp != '\0'){
if(*tmp < '' || *tmp >''){
if(inte){
return num;
}
else
return - * num;
}
tmp_add = num;
for(int i = ; i < ;i++){
if (num+tmp_add <num )
goto overnum;
num = num + tmp_add;
}
if(num +(*tmp -'') < num)
goto overnum;
num = num +(*tmp - '');
//num = num * 10 + (*tmp - '0');
compare_num = num;
tmp++;
}
if(inte){
return num;
}
else{
return - * num;
}
overnum:
if(inte) //正数
return ;
else
return -;
}
LeetCode.atoi的更多相关文章
- [LeetCode] 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) **
https://oj.leetcode.com/problems/string-to-integer-atoi/ 细节题,把一个字符串转换成整数 class Solution { public: in ...
- [LeetCode][Python]String to Integer (atoi)
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/string- ...
- 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) leetcode
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- Kotlin实现LeetCode算法题之String to Integer (atoi)
题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 class ...
- leetcode刷题笔记08 字符串转整数 (atoi)
题目描述 实现 atoi,将字符串转为整数. 在找到第一个非空字符之前,需要移除掉字符串中的空格字符.如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连续的数字组合起来,这部分字符即 ...
- 【一天一道LeetCode】#8. String to Integer (atoi)
一天一道LeetCode系列 (一)题目 Implement atoi to convert a string to an integer. Hint: Carefully consider all ...
- LeetCode(8):字符串转整数(atoi)
Medium! 题目描述: 实现 atoi,将字符串转为整数. 在找到第一个非空字符之前,需要移除掉字符串中的空格字符.如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连续的数字组合 ...
随机推荐
- windows系统下的特殊目录导致的FileNotFoundException
环境:下面只有JDK(内含jre),没有外在的jre 读取的两个文件都是存在的,只是文件名不同 运行结果1 运行结果2 切换JDK 运行结果3: 运行结果4: 请留意:C:\windows\syste ...
- Linux(Ubuntu)使用日记------Mongodb的安装与使用
1.安装 Linux下安装mongodb还是比较容易的 直接使用apt-get安装即可,命令如下: sudo apt-get install mongodb 安装完成之后进行检验, “mongo sh ...
- 在Asp.Net Core中集成ABP Dapper
在实际的项目中,除了集成ABP框架的EntityFrameworkCore以外,在有些特定的场景下不可避免地会使用一些SQL查询语句,一方面是由于现在的EntityFrameworkCore2.X有些 ...
- React笔记:组件(3)
1. 组件定义 组件是React的核心概念,组件将应用的UI拆分成独立的.可复用的模块. 定义组件的两种方式: (1)类组件:使用ES6 class (2)函数组件:使用函数 使用class定义组件的 ...
- react 入坑笔记(六) - 组件的生命周期
React 组件生命周期 详细参考: react 组件生命周期 组件的生命周期可分为三个状态: 1.Mounting:已经挂载/插入到真实 DOM 树上: 2.Updating:正在被重新渲染: 3. ...
- jQuery的事件处理
一.页面加载响应事件 $(document).ready()方法,获取文档就绪的时候.他极大地提高了Web相应速度.虽然该方法可以代替传统的window.onload()方法,但是两者之间仍然有差别. ...
- ISOMAP
转载 https://blog.csdn.net/dark_scope/article/details/53229427# 维度打击,机器学习中的降维算法:ISOMAP & MDS 降维是机器 ...
- 动态强制改变for循环里面item的值
var item = that.data.articles[inde_x]//获取数据列表 item.is_collection = 1//希望将特定的值改变 console.log(that.dat ...
- 函数中的this与argument对象,以及argument中的callee与caller属性
相关阅读:https://segmentfault.com/a/1190000015438195 相关阅读: https://zhuanlan.zhihu.com/p/23804247 相关阅读: h ...
- python类方法以及类调用实例方法的理解
classmethod类方法 1) 在python中.类方法 @classmethod 是一个函数修饰符,它表示接下来的是一个类方法,而对于平常我们见到的则叫做实例方法. 类方法的第一个参数cls,而 ...