博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
2-String to Integer (atoi)
阅读量:5082 次
发布时间:2019-06-13

本文共 3285 字,大约阅读时间需要 10 分钟。

实现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;//http://www.cnblogs.com/binyao/p/5026406.htmlnamespace 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 (str[i] == ' ' && (result == 0 && sign == 0))                {                    i++;                }                else if (str[i] == '+' && (result == 0 && sign == 0))                {                    sign = 1;                    i++;                }                else if (str[i] == '-' && (result == 0 && 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;        }    }}
View Code

 

转载于:https://www.cnblogs.com/binyao/p/5026406.html

你可能感兴趣的文章
小算法
查看>>
201521123024 《java程序设计》 第12周学习总结
查看>>
新作《ASP.NET MVC 5框架揭秘》正式出版
查看>>
IdentityServer4-用EF配置Client(一)
查看>>
WPF中实现多选ComboBox控件
查看>>
读构建之法第四章第十七章有感
查看>>
Windows Phone开发(4):框架和页 转:http://blog.csdn.net/tcjiaan/article/details/7263146
查看>>
Unity3D研究院之打开Activity与调用JAVA代码传递参数(十八)【转】
查看>>
python asyncio 异步实现mongodb数据转xls文件
查看>>
TestNG入门
查看>>
【ul开发攻略】HTML5/CSS3菜单代码 阴影+发光+圆角
查看>>
IOS-图片操作集合
查看>>
模板统计LA 4670 Dominating Patterns
查看>>
IO—》Properties类&序列化流与反序列化流
查看>>
session如何保存在专门的StateServer服务器中
查看>>
测试计划
查看>>
Mysql与Oracle 的对比
查看>>
jquery实现限制textarea输入字数
查看>>
thinkphp5 csv格式导入导出(多数据处理)
查看>>
PHP上传RAR压缩包并解压目录
查看>>