2-String to Integer (atoi)

实现atoi这个函数, public int atoi(String str),传入字符串str可以返回整数,请仔细考虑一下字符串的各种情况!

String to Integer: Case分析

  1. 正常数字

Sample:”123”,”0”,”1” ,"-1"

  1. 普通特殊字符:

Sample: "000","001","-001"

  1. 正负号问题

    1. 含有正负号

Sample: " -123", " +123", "-123", "+123","--123","++123","  -004500"

  1. 空格问题

    1. 含有空格

Sample: " 123","123 123","123 "

  1. 包含特殊符号

    1. 字母,其它特殊符号

Sample: "*123","*abc","~123","123~", "a123","12a3", "12+3","12-3"

  1. 空的字符串

Sample: string.Empty,null,""," "

  1. 边界问题

    1. 正常或者超过Int32最大和最小值,输出最大 或最小Int32

Sample: "-2147483648","2147483647"

Sample: "-214748364800","214748364700"

Snapshot:

Source Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StringtoInteger
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] str = { string.Empty,null,""," ",
                            "0","1","123","000","001","-1","-001",
                            " 123","123 123","123 ",
                            " -123", " +123",
                            "-123", "+123","--123","++123","  -004500",
                            "*123","*abc","~123","123~",
                            "a123","12a3",
                            "12+3","12-3",
                            "-2147483648","2147483647",
                            "-214748364800","214748364700" };
            StringtoInteger(str);
        }

        public static void StringtoInteger(string[] str)
        {
            Console.WriteLine("StringtoInteger Result is:");
            foreach (string s in str)
            {
                Console.Write("Test Data:{0}", s);
                Console.WriteLine("  Result:{0}", StringtoInteger(s));
            }
        }

        public static int StringtoInteger(string str)
        {
            int sign = 0;
            int i = 0;
            int result = 0;
            if (string.IsNullOrEmpty(str))
            {
                return result;
            }

            while (i < str.Length && ((str[i] >= ‘0‘ && str[i] <= ‘9‘) || str[i] == ‘ ‘ || str[i] == ‘-‘ || str[i] == ‘+‘))
            {
                if ((result == 0 && sign == 0) && str[i] == ‘ ‘)
                {
                    i++;
                }
                else if (str[i] == ‘+‘ && sign == 0)
                {
                    sign = 1;
                    i++;
                }
                else if (str[i] == ‘-‘ && sign == 0)
                {
                    sign = -1;
                    i++;
                }
                else if (str[i] >= ‘0‘ && str[i] <= ‘9‘)
                {
                    if (result > (int.MaxValue - (str[i] - ‘0‘)) / 10)
                    {
                        if (sign == 0 || sign == 1)
                            return int.MaxValue;
                        return int.MinValue;
                    }

                    result = result * 10 + str[i] - ‘0‘;
                    i++;
                }
                else
                {
                    if (sign == 0)
                        return result;
                    return result * sign;
                }
            }

            if (sign == 0)
                return result;
            return result * sign;
        }
    }
}

时间: 2024-10-19 03:04:08

2-String to Integer (atoi)的相关文章

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

[LeetCode][JavaScript]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

LeetCode【8】. String to Integer (atoi) --java实现

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

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

每日算法之八: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

【Leet Code】String to Integer (atoi) ——常考类型题

String to Integer (atoi) Total Accepted: 15482 Total Submissions: 106043My Submissions 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 yours

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