2-String to Integer (atoi)
实现atoi这个函数, public int atoi(String str),传入字符串str可以返回整数,请仔细考虑一下字符串的各种情况!
String to Integer: Case分析
- 正常数字
Sample:”123”,”0”,”1” ,"-1"
- 普通特殊字符:
Sample: "000","001","-001"
- 正负号问题
- 含有正负号
Sample: " -123", " +123", "-123", "+123","--123","++123"," -004500"
- 空格问题
- 含有空格
Sample: " 123","123 123","123 "
- 包含特殊符号
- 字母,其它特殊符号
Sample: "*123","*abc","~123","123~", "a123","12a3", "12+3","12-3"
- 空的字符串
Sample: string.Empty,null,""," "
- 边界问题
- 正常或者超过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.html namespace StringtoInteger
{
class Program
{
static void Main(string[] args)
{
string[] str = { string.Empty,null,""," ",
"","","","","","-1","-001",
"","123 123","123 ",
" -123", " +123",
"-123", "+123","--123","++123"," -004500",
"*123","*abc","~123","123~",
"a123","12a3",
"12+3","12-3",
"-2147483648","",
"-214748364800","" };
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 = ;
int i = ;
int result = ; if (string.IsNullOrEmpty(str))
{
return result;
} while (i < str.Length && ((str[i] >= '' && str[i] <= '') || str[i] == ' ' || str[i] == '-' || str[i] == '+'))
{
if (str[i] == ' ' && (result == && sign == ))
{
i++;
}
else if (str[i] == '+' && (result == && sign == ))
{
sign = ;
i++;
}
else if (str[i] == '-' && (result == && sign == ))
{
sign = -;
i++;
}
else if (str[i] >= '' && str[i] <= '')
{
if (result > (int.MaxValue - (str[i] - '')) / )
{
if (sign == || sign == )
return int.MaxValue;
return int.MinValue;
} result = result * + str[i] - '';
i++;
}
else
{
if (sign == )
return result;
return result * sign;
}
} if (sign == )
return result;
return result * sign;
}
}
}
2-String to Integer (atoi)的更多相关文章
- 【leetcode】String to Integer (atoi)
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- No.008 String to Integer (atoi)
8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...
- leetcode第八题 String to Integer (atoi) (java)
String to Integer (atoi) time=272ms accepted 需考虑各种可能出现的情况 public class Solution { public int atoi( ...
- leetcode day6 -- String to Integer (atoi) && 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 ...
- String to Integer (atoi) - 字符串转为整形,atoi 函数(Java )
String to Integer (atoi) Implement atoi to convert a string to an integer. [函数说明]atoi() 函数会扫描 str 字符 ...
- Kotlin实现LeetCode算法题之String to Integer (atoi)
题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 class ...
- LeetCode--No.008 String to Integer (atoi)
8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...
- leetcode-algorithms-8 String to Integer (atoi)
leetcode-algorithms-8 String to Integer (atoi) Implement atoi which converts a string to an integer. ...
- LeetCode: String to Integer (atoi) 解题报告
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- 《LeetBook》leetcode题解(8): String to Integer (atoi) [E]——正负号处理
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
随机推荐
- ftp 命令行操作 经常使用命令
> ftp <host> [port] > pwd # 查看当前文件夹 > dir # 查看FTPserver中的文件及文件夹 > mkdir <dirn ...
- (转)ngui3.5.7 版本Scroll View实现方法
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://xyo123.blog.51cto.com/6369437/1405861 现在网 ...
- 为什么要使用href=”javascript:void(0);”?
JavaScript中语句最后的分号是可以缺省的,那为何要使用javascript:;而不是javascript:呢? 是习惯还是规范,我疑惑了! 具有代码洁癖的coder们,没事多写一个分号,圣洁的 ...
- (算法)Trapping Rain Water I
题目: Given n non-negative integers representing an elevation map where the width of each bar is 1, co ...
- Linux之nohup命令:实现退出终端后程序继续后台运行
转自:http://tech.ccidnet.com/art/302/20070618/1115599_1.html 简单而有用的nohup命令在UNIX/LINUX中,普通进程用&符号放到后 ...
- 在网页浏览器中原生显示PDF文件
在网页中直接显示pdf格式的文件方便阅读.但是如果文件较大加载速度会很慢,另外如果客户端没有安装pdf阅读插件的话,也就看不了了. 这种方式的好处就是不需要转换,直接显示,而且在加载时(高级的浏览器, ...
- 几款Android开发人员必备小工具
在这里我介绍一下我常常在Android Studio里面使用的小工具吧,这些工具都能够在plugin里面搜索到. (当然了哈.我也是从网上找的.用着挺方便的,在这里总结一下) Gsonformat: ...
- 【SPSS】软件介绍
SPSS软件是美国斯坦福大学三位学生1968年研制开发的统计软件,SPSS是Statistical Package for Social Science(社会科学软件统计包)的缩写,2000年SPSS ...
- HTTP协议详解之User Agent篇
•User Agent:用户代理 指浏览器他的信息包括硬件平台.系统软件.应用软件和用户个人偏好.用户代理不仅仅指浏览器,还包括搜索引擎. •为什么所有浏览器的User Agent都带有Mozilla ...
- SpringMVC之ModelAndView的用法(转)
原文地址:https://blog.csdn.net/qq30211478/article/details/78016155 (一)使用ModelAndView类用来存储处理完后的结果数据,以及显示该 ...