把字符串转换成整数

题目描述

将一个字符串转换成一个整数,要求不能使用字符串转换整数的库函数。 数值为0或者字符串不是一个合法的数值则返回0
题目链接:https://www.nowcoder.com/practice/1277c681251b4372bdef344468e4f26e?tpId=13&tqId=11202&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking


public class Solution {
    public int StrToInt(String str) {
        if(str==null || str.length()==0){
            return 0;
        }
        boolean isNegative=str.charAt(0)=='-';
        long res=0;
        for(int i=0;i'9')
                return 0;
            res=res*10+(c-'0');
        }
        res = isNegative ? -res : res;
        if(res>Integer.MAX_VALUE|| res

你可能感兴趣的:(把字符串转换成整数)