Works on eclipse out of bounds via submission


  • 0
    M
        public int[] plusOne(int[] digits) {
        	res = new int[digits.length];
        	StringBuilder sb = new StringBuilder();
        	for (int i : digits) {
    			sb.append(i);
    		}
        	String intermediateString = sb.toString();
        	System.out.println("=>" +  intermediateString );
    		Integer val = Integer.valueOf( intermediateString  );
        	Integer plusOne = val + 1;
        	char[] almostThere = String.valueOf( plusOne ).toCharArray();
        	for (int i = 0; i < almostThere.length; i++) {
        		res[i ] = Integer.valueOf( Character.valueOf( almostThere[i]).toString() );
        	}
        	
        	return res;
    }

  • 1
    M

    Try testing it with the number 9 passed in. res is defined as having length equal to the length of the input string, so when the number gains a place, almostThere.length is 1 greater than res.length. That means that when i reaches almostThere.length-1, it tries to insert that index. This is res[res.length], which is an out of bounds error.


  • 0
    M

    revised but still no luck

    import org.omg.CORBA.OMGVMCID;

    public class NumDigitsPlus1 {
    public static class Solution {

    	public Solution () {
    		
    	}
        public int[] plusOne(int[] digits) {
           
        	StringBuilder sb = new StringBuilder();
        	for (int i : digits) {
    			sb.append(i);
    		}
        	String intermediateString = sb.toString();
        	System.out.println("=>" +  intermediateString );
        	BigInteger val = new BigInteger( intermediateString );
    		
        	BigInteger plusOne = val.add( new BigInteger("1") );
        	char[] almostThere = String.valueOf( plusOne ).toCharArray();
        	int[] res = new int[ almostThere.length ];
        	for (int i = 0; i < almostThere.length; i++) {
        		res[i ] = Integer.valueOf( Character.valueOf( almostThere[i]).toString() );
        	}
        	
        	return res;
        }
    }

  • 0
    M

    Still the same error? Or just not being accepted? If same error, make sure you've deleted the print statement.


  • 0
    M

    its a different error now, this time it says wrong result. when I pass an input of [0] on eclipse I get a single element int array back with 1 but the OJ says the code returns [0]


  • 0
    M

    It sounds like you're printing intermediateString somewhere. I've run your program and it seems to work, so I think you have that print happening. Leetcode takes the first output you provide, so prints take precedence over returned values.


  • 0
    M

    that was it, sorry didnt realise how OJ interprets the answer.


  • 0
    M

    any ideas on how to make this code better? how about the big O of this solution, it seems m*n it me since i am iterating over digits a few times


  • 0
    M

    This really should be a different question, not an answer to your current question. Make sure to describe your algorithm, and some ideas for how to improve it.


Log in to reply
 

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