Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ArrayList Removing first element

Tags:

java

This is the given question: Given a non-negative number represented as an array of digits,

add 1 to the number ( increment the number represented by the digits ).

The digits are stored such that the most significant digit is at the head of the list.

Example:

If the vector has [1, 2, 3]

the returned vector should be [1, 2, 4]

as 123 + 1 = 124.

This is my code:

 public class Solution {
    public ArrayList<Integer> plusOne(ArrayList<Integer> A) {       
        int carry = 1;
        int length = A.size();
        ArrayList result = new ArrayList();

        for( int i = length - 1; i >=0; i-- ){
            int val = A.get(i) + carry;
            result.add(0,val % 10);
            carry = val / 10;
        }

        if (carry == 1){
            result.add(0,1);
        }

        for (int j = 0; j < result.size(); j++){
            if(result.get(j).equals(0))
                result.remove(j);
            else
                break;
       }

        return result;

    }
  }

However, in the test case: A : [ 0, 6, 0, 6, 4, 8, 8, 1 ]

it says my function returns

6 6 4 8 8 2

while the correct answer is

6 0 6 4 8 8 2

I have no idea what is wrong with my code.

Thanks!

like image 697
Bill Cheng Avatar asked May 13 '15 04:05

Bill Cheng


3 Answers

if(result.get(j).equals(0))
    result.remove(j);
else
    break;

This will fail if every other index contains a 0. Here's what happens:

0 6 0 6 4 8 8 2
^ (j = 0)

The 0 will be removed, and j is incremented by one.

6 0 6 4 8 8 2
  ^ (j = 1)

Then this 0 is removed as well, skipping the first 6 in your array. To fix this, change the snippet to:

if(result.get(j).equals(0))
    result.remove(j--);
else
    break;

This compensates for when an index is removed so that j will not skip the number immediately after any removed 0s.

like image 169
Patrick Roberts Avatar answered Oct 09 '22 21:10

Patrick Roberts


Check out a similar question at Looping through and arraylist and removing elements at specified index

simpler to do just

while (!result.isEmpty() && result.get(0).equals(0)) {
  result.remove(0);
}

This will keep removing the left most 0 until there is no more left most zero to be deleted.

like image 23
Nat Avatar answered Oct 09 '22 22:10

Nat


Your last for loop is removing 0 from your result ArrayList<Integer>. After removing that loop, you will get perfect output

public static ArrayList<Integer> plusOne(ArrayList<Integer> A) {       
    int carry = 1;
    int length = A.size();
    ArrayList result = new ArrayList();

    for (int i = length - 1; i >= 0; i--) {
        int val = A.get(i) + carry; //2 8
        result.add(0, val % 10);    // 2 8
        carry = val / 10;  
    }

    if (carry == 1) {
        result.add(0, 1);
    }

//  for (int j = 0; j < result.size(); j++) {
//      if (result.get(j).equals(0))
//          result.remove(j);
//      else
//          break;
//  }

    for (boolean isZero = true; isZero; ) {
        isZero = result.get(0).equals(0);

        if(isZero)
            result.remove(0);
    }

    return result;
}
like image 24
Kushal Avatar answered Oct 09 '22 21:10

Kushal