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的更多相关文章

  1. LeetCode----8. String to Integer (atoi)(Java)

    package myAtoi8; /* * Implement atoi to convert a string to an integer. Hint: Carefully consider all ...

  2. leetcode8 String to Integer (atoi)

    题目需求: 输入一个字符串,输出对应的int值 特殊处理: 输入: null  输出:0 输入: "a122"  输出:0 输入: "   1233"  输出: ...

  3. [Swift]LeetCode8. 字符串转整数 (atoi) | String to Integer (atoi)

    Implement atoi which converts a string to an integer. The function first discards as many whitespace ...

  4. leetcode8:字符串转整数 (atoi)

    实现 atoi,将字符串转为整数. 在找到第一个非空字符之前,需要移除掉字符串中的空格字符.如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连续的数字组合起来,这部分字符即为整数的值 ...

  5. LeetCode8.字符串转整数(atoi)

    题目链接:https://leetcode-cn.com/problems/string-to-integer-atoi/ 实现 atoi,将字符串转为整数. 该函数首先根据需要丢弃任意多的空格字符, ...

  6. LeetCode8. 字符串转整数 (atoi)

    8. 字符串转整数 (atoi) 描述 实现 atoi,将字符串转为整数. 在找到第一个非空字符之前,需要移除掉字符串中的空格字符.如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连 ...

  7. LeetCode8.字符串转换整数(atoi) JavaScript

    请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们寻找到的第一个非空字符为正或者负号时,则将该符号与之 ...

  8. leetcode8 String to Integer

    题目描述 Implement atoi which converts a string to an integer. The function first discards as many white ...

  9. leetcode-8.atoi · string *

    题面 原题挺长的,还是英文,就不抄了,

随机推荐

  1. 【剑指offer】不用加减乘除做加法,C++实现

    原创博文,转载请注明出处! # 题目 # 思路 第一步:不考虑进位对每一位相加(异或操作) 第二步:考虑进位(位与运算+左移) 第三步:第一步和第二步相加(重复执行前两步) # 代码 #include ...

  2. phpcms修改增加编辑时摘要自动提取的数量

    \caches\caches_model\caches_data\model_field_1.cache.php 搜索 name="introcude_length" value= ...

  3. ios 延迟调用 && UIImageView && UILabel && UISegmentedControl && UISwitch && UISlider

    // //  ViewController.m //  UI_Lesson3 // //  Created by archerzz on 15/8/13. //  Copyright (c) 2015 ...

  4. 字符编码:ASCII,Unicode和UTF-8

    字符编码是计算机技术的基石,想要熟练使用计算机,就必须懂得一点字符编码的知识. 1. ASCII码 我们知道,在计算机内部,所有的信息最终都表示为一个二进制的字符串.每一个二进制位(bit)有0和1两 ...

  5. JLOI2019游记

    JLOI2019游记 DAY -??? 听说是12省联考,好刺激. DAY 1 看题 t1是个lydsy题我还写过博客,t2不会,t3一脸神仙. 这个t3数据好大啊,看到好几个人都用gedit打开大样 ...

  6. [MEF]第05篇 MEF的目录(Catalog)筛选

    一.演示概述本示例演示如何使用MEF提供的目录(Catalog)的扩展机制实现可过滤导出部件的自定义目录类.主要是通过继承ComposablePartCatalog基类,并实现接口INotifyCom ...

  7. java I/O进程控制,重定向 演示样例代码

    java I/O进程控制,重定向 演示样例代码 package org.rui.io.util; import java.io.*; /** * 标准I/O重定向 */ public class Re ...

  8. 使用Apache Archiva管理Maven仓库

    1 . 私服简介 私服是架设在局域网的一种特殊的远程仓库,目的是代理远程仓库及部署第三方构件.有了私服之后,当 Maven 需要下载构件时,直接请求私服,私服上存在则下载到本地仓库:否则,私服请求外部 ...

  9. UOJ #54 时空穿梭 —— 计数+莫比乌斯反演+多项式系数

    题目:http://uoj.ac/problem/54 10分还要用 Lucas 定理囧...因为模数太小了不能直接算... #include<cstdio> #include<cs ...

  10. 7.Python使用pandans遇到的坑

    1.开始入门Pandas,然后跟着网上的例子,编写以下代码: import pandas as pd import datetime import pandas.io.data as web star ...