Implement atoi to convert a string to an integer.
int atoi (const char * str);
Convert string to integer
Parses the C-string str interpreting its content as an integral number, which is returned as a value of type int. The function first discards as many whitespace characters (as in isspace) 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 base- 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 and zero is returned.
class Solution {
public:
int atoi(const char *str) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int len = strlen(str) ;
int i =;
long long result = ;
int flag = , b= ;
int numjia = ;
int numjian = ; while(i<len && str[i] == ' ') i++; for(; i< len ; i++)
{
switch(str[i])
{
case '+': numjia++;break;
case '-': numjian++; flag =-;break;
case '':
case '':
case '':
case '':
case '':
case '':
case '':
case '':
case '':
case '': result+=str[i] - '';result*=;break;
default: b = ;break;
}
if(b==) break;
if(numjia> ||numjian>)
return ;
} result = result/;
result *= flag; if(result > INT_MAX) return INT_MAX;
if(result < INT_MIN) return INT_MIN;
return result;
}
};

重写后:

class Solution {
public:
int atoi(const char *str) {
// Start typing your C/C++ solution below
// DO NOT write int main() function if(str == NULL ) return ; int len = strlen(str);
long long res = ;
int cur = , flag = ; while(cur<len && str[cur] == ' ') ++cur; if( str[cur] == '-' || str[cur] == '+'){
if(str[cur] == '-')
flag = -;
cur++;
} while(cur < len){
int c = str[cur] - '';
if(c< || c > )
break;
res = c + res*;
++cur;
}
res *= flag ; if(res > INT_MAX)
return INT_MAX;
if(res < INT_MIN)
return INT_MIN; return res;
}
};

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

随机推荐

  1. java cannot find the tag library descriptor for "http://java.sun.com/jsp/jstl/core"

    出现java cannot find the tag library descriptor for "http://java.sun.com/jsp/jstl/core" 主要的愿 ...

  2. 转:Android 测试 Appium、Robotium、monkey等框架或者工具对比

    原文地址:http://demo.netfoucs.com/u012565107/article/details/36419297# 1. Appium测试 (功能测试,用户接受度测试,黑盒测试) - ...

  3. jQuery 1.6+ 中attr()与prop() 区别

    最近在写一个关于checkbox全选与取消全选的优化方法时,看到很多高手用到了.prop(). 于是在jquery的帮助文档查了一下,才知道这是在jquery 1.6.1中新加的方法,用来设置属性.但 ...

  4. list 操作

    animals = ["aardvark", "badger", "duck", "emu", "fennec ...

  5. html checkbox全选或者全不选

    /* 全选或全不选 */ function CheckedAllOrNo() { var arr = $(':checkbox'); for (var i = 1; i < arr.length ...

  6. AngularJs学习笔记4——四大特性之双向数据绑定

    双向数据绑定 方向1:模型数据(model)绑定到视图(view) 实现方法:①.{{model变量名}}  ②.常用指令(ng-repeat) 方向2:将视图(view)中用户输入的数据绑定到模型数 ...

  7. Win32多线程编程(3) — 线程同步与通信

      一.线程间数据通信 系统从进程的地址空间中分配内存给线程栈使用.新线程与创建它的线程在相同的进程上下文中运行.因此,新线程可以访问进程内核对象的所有句柄.进程中的所有内存以及同一个进程中其他所有线 ...

  8. javasscript学习笔记 之 数组学习二 数组的所有方法

    1.push() 和 pop()  栈的方法 后进先出 push() 该方法是向数组末尾添加一个或者多个元素,并返回新的长度. push()方法可以接收任意数量的参数,把它们逐个添加到数组的末尾,并返 ...

  9. vsim生成VCD波形文件(verilog)

    Vsim(ModelSim)生成VCD波形文件(verilog) 两种方法 方法一: 调用ModelSim自己的命令生成,仿真脚本中加入如下一句即可 vcd file mytb.vcd 方法二: 调用 ...

  10. <.net>委托初探

    最近在学<.net深入体验与实战精要>. 今天就来初步讲解下委托. 一句话:委托定义了方法类型,可以将方法当做另一个方法的参数进行传递.委托包涵的只是方法的地址,而不是数据.类似于c指针. ...