String to Integer (atoi) ---- LeetCode 008
Implement atoi to convert a string to an integer.
Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.
Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.
Requirements for atoi:
The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.
The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.
If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.
If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.
Solution 1:
class Solution
{
public:
int myAtoi(string str)
{
int num = ;
int sign = ;
const int n = str.length(); int i = ;
while(str[i] == ' ' && i < n) i++; if(str[i] == '+') i++;
else if(str[i] == '-') { sign = -; i++; } for(; i < n; i++)
{
int temp = str[i] - '';
if(temp < || temp > )
break; if(sign == -)
{
if(num < (INT_MIN + temp) / )
return INT_MIN;
num = num * - temp; }
else
{
if(num > (INT_MAX - temp) / )
return INT_MAX;
num = num * + temp;
}
}
return num;
} };
Solution 2:
class Solution
{
public:
int myAtoi(string str)
{
int num = ;
int sign = ;
const int n = str.length(); int i = ;
while(str[i] == ' ' && i < n) i++; if(str[i] == '+') i++;
else if(str[i] == '-') { sign = -; i++; } for(; i < n; i++)
{
int temp = str[i] - '';
if(temp < || temp > )
break; temp = (sign == ) ? temp : -temp; // Attention if(sign == -)
{
if(num < (INT_MIN - temp) / )
return INT_MIN;
}
else
{
if(num > (INT_MAX - temp) / )
return INT_MAX;
}
num = num * + temp;
}
return num;
} };
String to Integer (atoi) ---- LeetCode 008的更多相关文章
- String to Integer (atoi) leetcode
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- String to Integer (atoi) leetcode java
题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...
- 8. String to Integer (atoi) ---Leetcode
Implement atoi to convert a string to an integer. 题目分析: 题目本身很简单就是将一个字符串转化成一个整数,但是由于字符串的千差万别,导致在实现的时候 ...
- 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 ...
- Kotlin实现LeetCode算法题之String to Integer (atoi)
题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 class ...
- LeetCode: String to Integer (atoi) 解题报告
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- 《LeetBook》leetcode题解(8): String to Integer (atoi) [E]——正负号处理
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)
Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...
- LeetCode 8. 字符串转换整数 (atoi)(String to Integer (atoi))
8. 字符串转换整数 (atoi) 8. String to Integer (atoi) 题目描述 LeetCode LeetCode8. String to Integer (atoi)中等 Ja ...
随机推荐
- HTML5自学笔记[ 8 ]历史管理
触发历史管理的三种方法: 跳转页面 改变hash值 pushState(在服务器环境下运行) 用hash值来触发历史管理: <!doctype html> <html lang=&q ...
- python 第三方模块 转 https://github.com/masterpy/zwpy_lst
Chardet,字符编码探测器,可以自动检测文本.网页.xml的编码. colorama,主要用来给文本添加各种颜色,并且非常简单易用. Prettytable,主要用于在终端或浏览器端构建格式化的输 ...
- Qt之可重入与线程安全
简述 本篇文章中,术语"可重入性"和"线程安全"被用来标记类与函数,以表明它们如何被应用在多线程应用程序中. 一个线程安全的函数可以同时被多个线程调用,甚至调用 ...
- java代码抓取网页邮箱
实现思路:1.使用java.net.URL对象,绑定网络上某一个网页的地址2.通过java.net.URL对象的openConnection()方法获得一个HttpConnection对象3.通过Ht ...
- 454. 4Sum II ——查找本质:hash最快,二分次之
Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...
- 和小猪一起搞微信公众号开发—获取Access_token
前言 前一篇小猪和大家分享了如何回复用户的简单文本,这一篇我们来看看如何获取Access_token 介绍 在前一篇中,我们实现了这么一个简单的过程:用户发送一个文本到公众号后,公众号在该文本后面加上 ...
- 项目构建工具Gradle的使用入门(参考,只表明地址)
Gradle入门介绍:简介 http://blog.jobbole.com/71999/ Gradle入门介绍:第一个Java项目 http://blog.jobbole.com/72558/ Gra ...
- js 数组去除空值
for(var i = 0 ;i<wordarr.length;i++) { if(wordarr[i] == "& ...
- android 计算器的实现(转)
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...
- bzoj 2744: [HEOI2012]朋友圈
#include<cstdio> #include<iostream> #define M 3010 using namespace std; ],u[M*M>>] ...