【LeetCode 8_字符串_实现】String to Integer (atoi)

 1 enum status{VALID = 0, INVALID};
 2 int g_status;
 3
 4 long long SubStrToInt(const char* str, bool minus)
 5 {
 6     long long num = 0;
 7     int flag = minus ? -1 : 1;
 8
 9     while (*str != ‘\0‘) {
10         if (*str >= ‘0‘ && *str <= ‘9‘) {
11             num = num * 10 + flag * (*str - ‘0‘);
12
13             if ((!minus && num > 0x7FFFFFFF)
14                 || (minus && num < (signed int)0x80000000)) {
15                 num = 0;
16                 break;
17             }
18             str++;
19         } else {
20             num = 0;
21             break;
22         }
23     }
24     if (*str == ‘\0‘)
25         g_status = VALID;
26
27     return num;
28 }
29
30 int StrToInt(const char* str)
31 {
32     g_status = INVALID;
33     long long num = 0;
34     bool minus = false;
35
36     if (str != NULL && *str != ‘\0‘) {
37         if (*str == ‘+‘) {
38             str++;
39         }
40         else if (*str == ‘-‘) {
41             minus = true;
42             str++;
43         }
44
45         if (*str != ‘\0‘)
46             num = SubStrToInt(str, minus);
47     }
48     return (int)num;
49 }

需要考虑的几个方面:

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

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

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

时间: 2024-10-26 21:26:44

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

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

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 spe

leetcode——String to Integer (atoi) 字符串转换为整型数(AC)

mnesia在频繁操作数据的过程可能会报错:** WARNING ** Mnesia is overloaded: {dump_log, write_threshold},可以看出,mnesia应该是过载了.这个警告在mnesia dump操作会发生这个问题,表类型为disc_only_copies .disc_copies都可能会发生. 如何重现这个问题,例子的场景是多个进程同时在不断地mnesia:dirty_write/2 mnesia过载分析 1.抛出警告是在mnesia 增加dump

leetCode 8. String to Integer (atoi) 字符串

8. String to Integer (atoi) 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 intende

LeetCode:String to Integer (atoi)

1.题目名称 String to Integer (atoi) (字符串到数字的转换) 2.题目地址 https://leetcode.com/problems/string-to-integer-atoi/ 3.题目内容 英文:Implement atoi to convert a string to an integer. 中文:实现atoi函数,将输入的字符串(String类型)转换为整型数据(Integer类型) 提示:实现的atoi函数需要满足以下特征 忽略字符串第一个非空格字符前的所

LeetCode 008 String to Integer (atoi)

[题目] 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 b

【leetcode系列】String to Integer (atoi)

这个我就直接上代码了,最开始把"abc123"也算作合法的了,后来查了一下atoi的定义,把这种去掉了. public class Solution { public static int atoi(String inStr) { long result = 0L; /* * 网上查了一下,atoi函数的定义是如果第一个非空格字符存在,是数字或者正负号则开始做类型转换, * 之后检测到非数字(包括结束符\0)字符时停止转换,返回整型数.否则,返回零.可能的输入情况有: 1.空字符串 *

LeetCode——String to Integer (atoi)

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 spe

Leetcode 数 String to Integer (atoi)

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie String to Integer (atoi) Total Accepted: 9862 Total Submissions: 67880 Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge,

【leetcode】String to Integer (atoi)

String to Integer (atoi) 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 f