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 *
题面 原题挺长的,还是英文,就不抄了,
随机推荐
- Linux运行环境搭建(一)——安装JDK
下载Linux版jdk 官网:http://www.oracle.com/technetwork/java/javase/downloads/index.html 解压并拷贝到指定目录 tar zxv ...
- tab页面自动跳转原因【在控制ul和li的时候没有细分】
效果图 存储buy的tab跳转js代码 $(function() { $('.tabPanel ul li').click(function(){ $(this).addClass('hit').si ...
- Java中有两种实现多线程的方式以及两种方式之间的区别
看到一个面试题.问两种实现多线程的方法.没事去网上找了找答案. 网上流传很广的是一个网上售票系统讲解.转发过来.已经不知道原文到底是出自哪里了. Java中有两种实现多线程的方式.一是直接继承Thre ...
- IOS SEL (@selector) 原理及使用总结(二)
SEL消息机制工作原理是什么 引用下面文章: 我们在之前有提到,一个类就像一个 C 结构.NSObject 声明了一个成员变量: isa. 由于 NSObject 是所有类的根类,所以所有的对象都会有 ...
- Java 并发:volatile 关键字解析
摘要: 在 Java 并发编程中,要想使并发程序能够正确地执行,必须要保证三条原则,即:原子性.可见性和有序性.只要有一条原则没有被保证,就有可能会导致程序运行不正确.volatile关键字 被用来保 ...
- LeetCode 529. Minesweeper
原题链接在这里:https://leetcode.com/problems/minesweeper/description/ 题目: Let's play the minesweeper game ( ...
- flex 伸缩布局
伸缩布局 布局的传统解决方案,基于盒状模型,依赖 display属性 + position属性 + float属性.它对于那些特殊布局非常不方便.CSS3在布局方面做了非常大的改进,使得我们对块级元素 ...
- LG1155 双栈排序
题意 Tom最近在研究一个有趣的排序问题.如图所示,通过2个栈\(S_1\)和\(S_2\),Tom希望借助以下4种操作实现将输入序列升序排序. 操作a 如果输入序列不为空,将第一个元素压入栈\(S_ ...
- check sub-string in the string
if "blah" not in somestring: continue
- 使用vigil 监控微服务系统包含可视化界面
1. 安装 a. rust cargo cargo install vigil-server b. docker docker pull valeriansaliou/vigil:v1.3.0 2. ...