enum status{VALID = , INVALID};
int g_status; long long SubStrToInt(const char* str, bool minus)
{
long long num = ;
int flag = minus ? - : ; while (*str != '\0') {
if (*str >= '' && *str <= '') {
num = num * + flag * (*str - ''); if ((!minus && num > 0x7FFFFFFF)
|| (minus && num < (signed int)0x80000000)) {
num = ;
break;
}
str++;
} else {
num = ;
break;
}
}
if (*str == '\0')
g_status = VALID; return num;
} int StrToInt(const char* str)
{
g_status = INVALID;
long long num = ;
bool minus = false; if (str != NULL && *str != '\0') {
if (*str == '+') {
str++;
}
else if (*str == '-') {
minus = true;
str++;
} if (*str != '\0')
num = SubStrToInt(str, minus);
}
return (int)num;
}

需要考虑的几个方面:

1、输入的字符串表示正数、负数和0;

2、边界值,最大的正整数和最小的负整数;

3、输入字符串为NULL、空字符串、字符串中有非数字、字符串开始的一段有空格等。

【LeetCode 8_字符串_实现】String to Integer (atoi)的更多相关文章

  1. 【LeetCode每天一题】String to Integer (atoi)(字符串转换成数字)

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

  2. 【Leetcode】【Easy】String to Integer (atoi)

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

  3. 【leetcode刷题笔记】String to Integer (atoi)

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

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

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

  5. LeetCode 8. 字符串转换整数 (atoi)(String to Integer (atoi))

    8. 字符串转换整数 (atoi) 8. String to Integer (atoi) 题目描述 LeetCode LeetCode8. String to Integer (atoi)中等 Ja ...

  6. 【leetcode】String to Integer (atoi)

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

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

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

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

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

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

随机推荐

  1. 利用HBase的快照功能来修改表名

    hbase的快照功能常常被用来做数据的恢复的,但是由于项目的特殊需求需要改hbase表的表名.在官网上通过快照功能来修改hbase表名的用法: 下面展示用shell命令的和Java api两种方式: ...

  2. 史上最全的MonkeyRunner自动化测试从入门到精通(3)

    原文地址https://blog.csdn.net/liu_jing_hui/article/details/60956088 MonkeyRunner复杂的功能开始学习 (1)获取APK文件中ID的 ...

  3. SLG手游Java服务器的设计与开发——数据管理

    文章版权归腾讯GAD所有,禁止匿名转载:禁止商业使用:禁止个人使用. 一.前言 上文介绍了我们的SLG手游的服务器架构设计以及网络通信部分,本文介绍数据管理部分,在数据存储方面,我选择了Mysql.M ...

  4. 【转】ViewPager 一屏显示多个子页面

    一.概述 项目中遇到一个需求:ViewPager 一屏显示多个子页面.因为之前没有做过这样的界面,所以经历了些许小插曲,特以记之! 主要内容来自: http://blog.csdn.net/JM_be ...

  5. BUCT20180814邀请赛 Solution

    A:SUM 水. #include<bits/stdc++.h> using namespace std; #define N 100010 typedef long long ll; i ...

  6. PowerDesigner教程系列

    文章转载至:http://www.cnblogs.com/yxonline/archive/2007/04/09/705479.html PowerDesigner教程系列(一)概念数据模型 目标:本 ...

  7. JavaScript 预编译(变量提升和函数提升的原理)

    本文部分内容转自https://www.cnblogs.com/CBDoctor/p/3745246.html 1.变量提升 console.log(global); // undefined var ...

  8. js 变量、函数提升 与js的预编译有关

    参考网址:http://www.codesec.net/view/178491.html 先简单理解下作用域的概念,方便对变量与函数提升的概念的理解 function foo() { var x = ...

  9. 20145325张梓靖 《Java程序设计》第7周学习总结

    20145325张梓靖 <Java程序设计>第7周学习总结 教材学习内容总结 时间的度量 格林威治时间,简称GMT时间,由观察太阳而得来:世界时,UT:国际原子时,TAI:世界协调时间,U ...

  10. css 基础 - 2

    css 基础 - 2 一.文本样式: 文字竖着书写: 语法:writing-mode : lr-tb.tb-rl 参数:lr-tb:从左向右,从上往下 tb-rl:从上往下,从右向左 1.text-a ...