当前位置:网站首页>字符串转换整数 (atoi)
字符串转换整数 (atoi)
2022-08-09 12:00:00 【爱敲代码的Harrison】
题目
力扣链接:字符串转换整数 (atoi)
代码
package com.lt.tiq01;
/** * @author Harrison * @create 2022-06-26-11:20 * @motto 众里寻他千百度,蓦然回首,那人却在灯火阑珊处。 */
public class P006_StringToIntegerAtoi {
public int myAtoi(String s) {
if(s==null || s.equals("")){
return 0;
}
// trim()把字符串的空格去掉,一个系统函数
s=removeHeadZero(s.trim());
if(s==null || s.equals("")){
return 0;
}
char[] str=s.toCharArray();
if(!isValid(str)){
return 0;
}
// str 是符合日常书写的,正经整数形式
// 为什么一定要转成负数的形式接着?
// -2147483648
// 2147483647
// 因为负数的绝对值比正数的绝对值大1,如果硬要拿正数接着
// 那么字符串"2147483648"就没法转换了,但其实按要求应转换为系统最大2147483647
// 因为是用负数的形式接着,所以不会溢出
boolean posi=str[0]=='-'?false:true;
int minq=Integer.MIN_VALUE/10;
int minr=Integer.MIN_VALUE%10;
int res=0;
int cur=0;
for(int i=(str[0]=='-' || str[0]=='+')?1:0; i<str.length; i++){
cur='0'-str[i];
if(res<minq || (res==minq && cur<minr)){
// 正数溢出返回系统最大,负数溢出返回系统最小
return posi?Integer.MAX_VALUE:Integer.MIN_VALUE;
}
res=res*10+cur;
}
// "-2147483648" -> -2147483648
// "2147483647" -> 2147483647
// res是用负数形式表达的
// "2147483648" -> 2147483647
// 下面if的情况,res是正数的形式,但却是系统最小,不会溢出,因为使用负数形式接着的
// 这种情况下转成系统最大
if(posi && res==Integer.MIN_VALUE){
return Integer.MAX_VALUE;
}
return posi?-res:res;
}
public static String removeHeadZero(String str){
boolean r=(str.startsWith("+") || str.startsWith("-"));
// 如果原字符串第一个字符包含'+'或'-',那就从第二个字符开始找第一个不是'0'的字符
int s=r?1:0;
for(; s<str.length(); s++){
if(str.charAt(s)!=0){
// 找到第一个不是字符'0'的位置
break;
}
}
// s 到了第一个不是'0'字符的位置
int e=-1;
// 左<-右(从右往左找第一个不是数字字符的位置)
for(int i=str.length()-1; i>=(r?1:0); i--){
if(str.charAt(i)<'0' || str.charAt(i)>'9'){
e=i;
}
}
// e 到了最左的 不是数字字符的位置
// substring(s,e) 左闭右开[s,e)
// 如果e==-1,说明s后面都是数字字符
// 再加上第一个字符'+'或者'-',如果有的话
return (r?String.valueOf(str.charAt(0)):"")+str.substring(s,e==-1?str.length():e);
}
// isValid() 检查是否是有效的整数
public static boolean isValid(char[] chas){
if(chas[0]!='-' && chas[0]!='+' && (chas[0]<'0' || chas[0]>'9')){
return false;
}
if((chas[0]=='-' || chas[0]=='+') && chas.length==1){
return false;
}
// 上面两个if没中,只剩下三张情况
// 0 +... -... num
// 0位置字符是'+',后面是数字字符
// 0位置字符是'-',后面是数字字符
// 0位置就是数字字符,后面也是
// 然后接着从1位置开始检查,如果中间有任何不是数字字符,直接return false
for(int i=1; i<chas.length; i++){
if(chas[i]<'0' || chas[i]>'9'){
return false;
}
}
// 经过上面的层层过滤,剩下的字符串一定是符合我们日常书写的,正经的整数形式
return true;
}
}
边栏推荐
猜你喜欢
罗振宇折戟创业板/ B站回应HR称用户是Loser/ 腾讯罗技年内合推云游戏掌机...今日更多新鲜事在此...
学长告诉我,大厂MySQL都是通过SSH连接的
【面试高频题】可逐步优化的链表高频题
Shell之常用小工具(sort、uniq、tr、cut)
已解决IndentationError: unindent does not match any oute r indentation Level
两分钟录音就可秒变语言通!火山语音音色复刻技术如何修炼而成?
索引index
用皮肤“听”音乐,网友戴上这款装备听音乐会:仿佛住在钢琴里
Double pointer - the role of char **, int **
win10编译x264库(也有生成好的lib文件)
随机推荐
Common gadgets of Shell (sort, uniq, tr, cut)
微信支付开发流程
proto3-2语法
WPF implements a MessageBox message prompt box with a mask
IDEA close/open reference prompt Usages
Two minutes recording can pass by second language!The volcano how to practice and become voice tone reproduction technology?
听声辨物,这是AI视觉该干的???|ECCV 2022
Django cannot link mysql database
Summary of learning stages (knapsack problem)
"Digital Economy Panorama White Paper" Special Analysis of Banking Industry Intelligent Marketing Application Released
WeChat payment development process
2022 Niu Ke Duo School (6) M. Z-Game on grid
WeChat Mini Program Payment and Refund Overall Process
报告:想学AI的学生数量已涨200%,老师都不够用了
C# 获取系统已安装的.NET版本
We really need DApp?Really can't meet our fantasy App?
Experiment record: the process of building a network
阻塞、非阻塞、多路复用、同步、异步、BIO、NIO、AIO 一锅端
How should the acceptance criteria for R&D requirements be written?| Agile Practices
Programmer's Exclusive Romance - Use 3D Engine to Realize Fireworks in 5 Minutes