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. CPU卡中T=0通讯协议的分析与实现

    IC卡的应用越来越广泛,从存储卡到逻辑加密卡,目前CPU卡已经逐渐在应用中占据主导地位.CPU卡根据通讯协议可分为两种:接触式和非接触式.接触式CPU卡主要采用两种通讯协议:T=0和T=1通讯协议.T ...

  2. KEIl混合编程步骤详解

    一.在keil中C函数调用汇编函数: 主要思路:先用C来编写所要实现及调用的汇编函数,然后由此C函数生成相应的汇编代码,这样我们就可以不用去管混合编程调用时复杂的函数接口,我们只要修改相应汇编函数中的 ...

  3. Borland license information was found,but it is not valid for delphi.

    The start Delphi7 come amiss: Borland license information was found,but it is not valid for delphi. ...

  4. SmartBusinessDevFramework架构设计-2:结构图示

    架构设计一览图 下图表示了本架构的设计草稿. 接下来  ,我们将逐步细述,各个模块之间的松散耦合关系. 核心的实现原理.敬请关注.

  5. AspNetPager实现真分页+多种样式

    真假分页 分页是Web应用程序中最常用到的功能之一.当从数据库中获取的记录远远超过界面承载能力的时候,使用分页可以使我们的界面更加美观,更加的用户友好.分页包括两种类型:真分页和假分页. 其中假分页就 ...

  6. Android App Widget的简单使用

    App Widget是一些桌面的小插件,比如说天气和某些音乐播放应用,放到桌面去的那部分: 例如: 实现步骤及代码如下: (1)首先,在AndroidManifest.xml中声明一个App Widg ...

  7. Asp.Net 构架(Http Handler 介绍) - Part.2

    原文地址:http://www.cnblogs.com/JimmyZhang/archive/2007/09/15/894124.html 引言 在 Part.1 Http请求处理流程 一文中,我们了 ...

  8. 如何改变Myeclipse编辑区背景色

    编辑窗口右键单击——>Preferences——>General加号——>Editors加号——>点Text Editors字样——>右下窗口选Backgroud col ...

  9. poj 1466 Girls and Boys(二分匹配之最大独立集)

    Description In the second year of the university somebody started a study on the romantic relations ...

  10. struts配置,略记

    <!-- <listener> <listener-class>org.springframework.web.context.ContextLoaderListener ...