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.

 class Solution {
public:
int atoi(const char *str) {
long long m=;//long long 判断溢出
int n=,flag1=,flag2=,i=;//flag1,flag2 对'+','-' 计数,i是总数,符号超过1个 出错
while(*str&&*str==' ')
++str;
while(*str=='+'||*str == '-'){
if(*str =='+'){
++flag1;
++i;
++str;
}else{
++flag2;
++i;
++str;
}
}
while(*str>&&*str<){
n = *str -'';
m *=;
m += n;
++str;
}
if(i>)
m= ;
else if(flag2==){
m *= -;
}
if(m>)
m = ;
else if(m < -)
m = -;
return m;
}
};

String to Integer (atoi)的更多相关文章

  1. 【leetcode】String to Integer (atoi)

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

  2. No.008 String to Integer (atoi)

    8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...

  3. leetcode第八题 String to Integer (atoi) (java)

    String to Integer (atoi) time=272ms   accepted 需考虑各种可能出现的情况 public class Solution { public int atoi( ...

  4. 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 ...

  5. String to Integer (atoi) - 字符串转为整形,atoi 函数(Java )

    String to Integer (atoi) Implement atoi to convert a string to an integer. [函数说明]atoi() 函数会扫描 str 字符 ...

  6. Kotlin实现LeetCode算法题之String to Integer (atoi)

    题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 class ...

  7. LeetCode--No.008 String to Integer (atoi)

    8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...

  8. leetcode-algorithms-8 String to Integer (atoi)

    leetcode-algorithms-8 String to Integer (atoi) Implement atoi which converts a string to an integer. ...

  9. LeetCode: String to Integer (atoi) 解题报告

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

  10. 《LeetBook》leetcode题解(8): String to Integer (atoi) [E]——正负号处理

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

随机推荐

  1. 一个端口操作类PortHelper

    此操作类主要使用了系统命令行来实现的,详细的命令可参照微软文档<如何使用"netsh advfirewall firewall"上下文而非“netsh firewall”上下 ...

  2. jquery function Optional Arguments

    1.javascript 选项散列对象 function Test(p1,p2,p3,p4,p5){ //do something } call: 参数可选 Test({ p1:value1, p2: ...

  3. PHP学习笔记:利用百度api实现手机归属地查询

    从来没有用过api,都不知道怎么获得api的数据,跟着demo,然后修改,终于实现了手机号码查询的功能,代码和说明很全,大家试试. <?php /** * Created by jianqing ...

  4. java入门基础知识点总结

    JavaScript他是一种描述性语言,其实他并不难学,只要用心学,一定会学好,我相信大家在看这篇文章的时候,一定也学过HTML吧,使用JavaScript就是为了能和网页有更好的交互,下面切入主题. ...

  5. Java中的Set集合接口实现插入对象不重复的原理

    在java的集合中,判断两个对象是否相等的规则是: 1).判断两个对象的hashCode是否相等 .      如果不相等,认为两个对象也不相等,完毕       如果相等,转入2)(这一点只是为了提 ...

  6. (七)play之yabe项目【CRUD】

    (七)play之yabe项目[CRUD] 博客分类: 框架@play framework    增加CRUD功能 使用CRUD能干嘛?----> 在页面对模型进行增删改查操作,这样有什么实际意义 ...

  7. osx的终端软件iterm2 之 修改外观 和 常用快捷键小结

    1.修改外观:透明,自己配色,最好还有个透明的小背景,比如这样: 那么你就要这样修改: 2.快捷键小结 (1)⌘ + d 横着分屏 / ⌘ + shift + d 竖着分屏  : 适合多操作的时候 ( ...

  8. 关于原生的Javascript

    JQuery是个好工具,它做了太多的事. 以至于让人渐渐忘记原生的JS该怎么写了,导致连为了用个DOM选择器或者Ajax就直接加个JQuery,确实,JQuery太方便了. 坏处: 由于JQuery的 ...

  9. rails provide与content_for的区别

    页面渲染时:provide先执行,但找到一个provide之后就不再查找 content_for 顺序执行,在哪个位置,就等之前的渲染完后才执行.但是要等到所有的content被查找完后一块返回,也就 ...

  10. andriod 动态显示当前时间

    <?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:android=&quo ...