8. String to Integer (atoi)
Medium

Implement atoi which converts a string to an integer.

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.

Note:

  • Only the space character ' ' is considered as whitespace character.
  • Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231,  231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned.

Example 1:

Input: "42"
Output: 42

Example 2:

Input: "   -42"
Output: -42
Explanation: The first non-whitespace character is '-', which is the minus sign.
  Then take as many numerical digits as possible, which gets 42.

Example 3:

Input: "4193 with words"
Output: 4193
Explanation: Conversion stops at digit '3' as the next character is not a numerical digit.

Example 4:

Input: "words and 987"
Output: 0
Explanation: The first non-whitespace character is 'w', which is not a numerical
  digit or a +/- sign. Therefore no valid conversion could be performed.

Example 5:

Input: "-91283472332"
Output: -2147483648
Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer.
  Thefore INT_MIN

 class Solution {
public:
int in(char ch){
if(ch<=''&&ch>='') return ;
else if(ch==' ')return ;
else if(ch=='-')return ;
else if(ch=='+') return ;
else return ;
}
int myAtoi(string str) {
int len = str.length();
long long ans = ;
int fl = ;//是否是负数
bool begin = ;
for(int i = ; i < len; i++){
if(in(str[i])==){
if(begin==) return ;
else break;
}
else if(in(str[i])==){
if(begin==){
fl = ;
begin = ;
}
else break;
}
else if(in(str[i])==){
if(begin==){
begin = ;
}
else break;
}
else if(in(str[i])==){
if(begin==)continue;
else break;
}
else{
ans = ans * + (str[i]-'');
begin = ;
if(ans >= pow(,)) {
break;
}
}
}
if(fl==) ans = -ans;
if(ans < -) return -;
if(ans >= ) return ;
return ans;
}
};

Leetcode 8. String to Integer (atoi)(模拟题,水)的更多相关文章

  1. Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)

    Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...

  2. leetcode day6 -- String to Integer (atoi) &amp;&amp; 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 ...

  3. 【leetcode】String to Integer (atoi)

    String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...

  4. [leetcode] 8. String to Integer (atoi) (Medium)

    实现字符串转整形数字 遵循几个规则: 1. 函数首先丢弃尽可能多的空格字符,直到找到第一个非空格字符. 2. 此时取初始加号或减号. 3. 后面跟着尽可能多的数字,并将它们解释为一个数值. 4. 字符 ...

  5. [LeetCode][Python]String to Integer (atoi)

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/string- ...

  6. [LeetCode] 8. String to Integer (atoi) 字符串转为整数

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

  7. LeetCode——8. String to Integer (atoi)

    一.题目链接:https://leetcode.com/problems/string-to-integer-atoi/ 二.题目大意: 实现一个和C语言里atoi具有相同功能的函数,即能够把字符串转 ...

  8. 【LeetCode】String to Integer (atoi) 解题报告

    这道题在LeetCode OJ上难道属于Easy.可是通过率却比較低,究其原因是须要考虑的情况比較低,非常少有人一遍过吧. [题目] Implement atoi to convert a strin ...

  9. [LeetCode] 8. String to Integer (atoi) ☆

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

随机推荐

  1. ugui点击穿透判断

    using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Eve ...

  2. Canvas入门08-绘制仪表盘

    需求 实现下图所示的仪表盘的绘制. 分析 我们先来将仪表盘进行图形拆分,并定义尺寸. 我们绘制的逻辑: 绘制中心圆 绘制环外圈圆 绘制环内圈圆 绘制刻度内圈圆 绘制刻度线 绘制刻度文字 绘制指针 定义 ...

  3. 系统用户与用户组管|chfn、密码管理、身份切换、sudo

    2 系统用户与用户组管理 GID为GroupId,即组ID,用来标识用户组的唯一标识符 UID为UserId,即用户ID,用来标识每个用户的唯一标示符 /etc/passwd /etc/shadow ...

  4. HashMap中确定数组位置为什么要用hash进行扰动

    HashMap数据存储的过程先根据key获得hash值,通过 (n - 1) & hash 判断当前元素存放的位置(这里的 n 指的是数组的长度),如果当前位置存在元素的话,就判断该元素与要存 ...

  5. 教你在 IntelliJ IDEA 中使用 VIM!

    Java技术栈 www.javastack.cn 优秀的Java技术公众号 IdeaVim(下载)插件可以让你在IntelliJ IDEA中键盘敲的飞起. 安装 打开IDEA的设置,在Plugins里 ...

  6. Scrapy 教程(八)-分布式爬虫

    scrapy 本身并不是一个分布式框架,而 Scrapy-redis 库使得分布式成为可能: Scrapy-redis 并没有重构框架,而是基于redis数据库重写了框架的某些组件. 分布式框架要解决 ...

  7. 简述COOKIE和SESSION的区别与联系?

    cookie 和session 的区别:1.cookie数据存放在客户的浏览器上,session数据放在服务器上.2.cookie不是很安全,别人可以分析存放在本地的COOKIE并进行COOKIE欺骗 ...

  8. PCIeの数据链路层与物理层详解

    数据链路层(DLL,Data Link Layer)的主要作用是进行链路管理(Link Management).TLP错误校验.Flow Control(流控制)和Link功耗管理.不仅可以接收发送来 ...

  9. FreeIPA部署及基本使用

    FreeIPA是一个集成安全信息管理解决方案,FreeIPA服务器通过存储管理计算机网络安全方面所需的用户.组.主机和其他对象的数据,提供集中的身份验证.授权和账户信息.结合了Linux.Direct ...

  10. django基础篇02-url路由系统

    django的路由系统: 一.基本用法: 1.path('index', views.index), # 通过类的方式创建url映射 2.path('home', views.Home.as_view ...