leetcode8
public class Solution {
public int MyAtoi(string str) {
int index = , sign = , total = ;
//1. Empty string
if (str.Length == )
{
return ;
}
//2. Remove Spaces
while (str[index] == ' ' && index < str.Length)
{
index++;
} //3. Handle signs
if (str[index] == '+' || str[index] == '-')
{
sign = str[index] == '+' ? : -;
index++;
} //4. Convert number and avoid overflow
while (index < str.Length)
{
int digit = str[index] - '';
if (digit < || digit > ) break; //check if total will be overflow after 10 times and add digit
if (int.MaxValue / < total || int.MaxValue / == total && int.MaxValue % < digit)
{
return sign == ? int.MaxValue : int.MinValue;
} total = * total + digit;
index++;
}
return total * sign;
}
}
https://leetcode.com/problems/string-to-integer-atoi/#/description
补充一个python的实现:
class Solution:
def myAtoi(self, string: str) -> int:
string = string.strip()
n = len(string)
if n == :
return
sign =
convertStr = ''
firstNum = False
for i in range(n):
c = ord(string[i]) - ord('')
if not firstNum:
if string[i] == '+' and sign == :
sign =
elif string[i] == '-' and sign == :
sign = -
elif c >= and c <= :
firstNum = True
if sign == :
sign =
convertStr += str(c)
else:
convertStr = ''
break
else:
if c >= and c <= :
convertStr += str(c)
else:
break
r = int(convertStr) * sign
if r > ** - :
r = ** -
elif r < -( ** ):
r = -( ** )
return r
leetcode8的更多相关文章
- LeetCode----8. String to Integer (atoi)(Java)
package myAtoi8; /* * Implement atoi to convert a string to an integer. Hint: Carefully consider all ...
- leetcode8 String to Integer (atoi)
题目需求: 输入一个字符串,输出对应的int值 特殊处理: 输入: null 输出:0 输入: "a122" 输出:0 输入: " 1233" 输出: ...
- [Swift]LeetCode8. 字符串转整数 (atoi) | String to Integer (atoi)
Implement atoi which converts a string to an integer. The function first discards as many whitespace ...
- leetcode8:字符串转整数 (atoi)
实现 atoi,将字符串转为整数. 在找到第一个非空字符之前,需要移除掉字符串中的空格字符.如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连续的数字组合起来,这部分字符即为整数的值 ...
- LeetCode8.字符串转整数(atoi)
题目链接:https://leetcode-cn.com/problems/string-to-integer-atoi/ 实现 atoi,将字符串转为整数. 该函数首先根据需要丢弃任意多的空格字符, ...
- LeetCode8. 字符串转整数 (atoi)
8. 字符串转整数 (atoi) 描述 实现 atoi,将字符串转为整数. 在找到第一个非空字符之前,需要移除掉字符串中的空格字符.如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连 ...
- LeetCode8.字符串转换整数(atoi) JavaScript
请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们寻找到的第一个非空字符为正或者负号时,则将该符号与之 ...
- leetcode8 String to Integer
题目描述 Implement atoi which converts a string to an integer. The function first discards as many white ...
- leetcode-8.atoi · string *
题面 原题挺长的,还是英文,就不抄了,
随机推荐
- Git详解之五 分布式Git
以下内容转载自:http://www.open-open.com/lib/view/open1328070090108.html 分布式 Git 为了便于项目中的所有开发者分享代码,我们准备好了一台服 ...
- Ubuntu12.04中在桌面建立指向网页的链接文件的方法
#cd /usr/share/applications #cp firefox.desktop ~/Desktop #cd ~/Desktop #chmod +x firefox.desktop 右键 ...
- MAC OS环境下搭建基于Python语言的Selenium2自动化测试环境
#1安装Python Mac OS上自带python2.7,在此介绍安装python3.x版本 去官网下载Python for MAC版本 https://www.python.org 安装文件为pk ...
- hive表数据导出到csv乱码原因及解决方案
转载自http://blog.csdn.net/lgdlxc/article/details/42126225 Hive表中的数据使用hive - e"select * from table ...
- python调用rpc实现分布式系统
rpc 一般俗称,远程过程调用,把本地的函数,放到远端去调用. 通常我们调用一个方法,譬如: sumadd(10, 20),sumadd方法的具体实现要么是用户自己定义,要么存在于该语言的库函数中,也 ...
- mui.fire 目标页无法监听到 触发事件
//获得详情页面 if(!detailPage){ detailPage = plus.webview.getWebviewById('detail.html'); } //触发详情页面的newsId ...
- worldpress自定义页面
一:wordpress制作自定义页面的方法 有时候我们需要制作一些个性化的页面,而不是直接用wordpress的page页面模板.这时候我们就需要自已写一个页面出来.下面介绍一下制作流程: 第一步:制 ...
- Wordpress主题站
深度剖析WordPress主题结构 http://down.chinaz.com/try/201106/640_1.htm wordpress工作原理 http://blog.csdn.net/liu ...
- 老齐python-基础9(函数)
继续上篇 函数 多参数: >>> def foo(x,y,z,*args,**kargs): ... print(x) ... print(y) ... print(z) ... p ...
- HotSpot Stop-and-Copy GC
rednaxelafx的Cheney算法的伪代码.如果不用forwarding的话,维护一个旧地址到新地址的映射也可以. 其中重点部分: void Heap::collect() { // The f ...