Leetcode is failing but on system eclipse running successfully


  • 0
    M

    Testcase: input "-00134" is failing saying output is 134 but expected is -134. When I am running on my system. I am getting -134. What could be reason for it.

    public static int myAtoi(String str) {
    
    	int sign = 1;
    	long intValue = 0;
    	if (str.length() > 0) {
    		char signC = str.charAt(0);
    		int startIndex = 0;
    		if (signC == '-') {
    			sign = -1;
    			startIndex = 1;
    		} else {
    			startIndex = 0;
    		}
    		if (str.length() > 2) {
    			if (str.charAt(0) == '-' && str.charAt(1) == '+'
    					|| str.charAt(0) == '+' && str.charAt(1) == '-')
    				return 0;
    		}
    		for (int i = startIndex; i < str.length(); i++) {
    			if (Character.isDigit(str.charAt(i))) {
    				intValue = intValue * 10 + (str.charAt(i)) - 48;
    			}
    		}
    		intValue = intValue * sign;
    		if (intValue < Integer.MAX_VALUE && intValue > Integer.MIN_VALUE)
    			return (int) intValue;
    		else if (intValue > Integer.MAX_VALUE)
    			return Integer.MAX_VALUE;
    		else if (intValue < Integer.MIN_VALUE)
    			return Integer.MIN_VALUE;
    	} else
    		return 0;
    	return 0;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.