-3
public class Solution implements Comparator<Integer> {

    public String largestNumber(final List<Integer> A) {
        List<Integer> B = A;
        StringBuilder str = new StringBuilder();
        Collections.sort(B,this);
        for (int i=B.size()-1; i >= 0; i--) {
            str = str.append(B.get(i));
        }
        return str.toString();
    }

    public int compare(Integer X, Integer Y) {
        String XY = Integer.toString(X);
        String YX = Integer.toString(Y);
        String QW = XY + YX;
        String WE = YX + XY;
        return QW.compareTo(WE);
    }
}

The program is complied but it fails for the test case array with input 00000.It says function is returning 00000 instead of 0.

Turing85
  • 18,217
  • 7
  • 33
  • 58
npy
  • 3
  • 2
  • 4
    Why do you return a String instead of an int? I also suggest that you learn the difference between Integer and int. – Code-Apprentice Dec 30 '17 at 19:20
  • 1
    A remark on your code: in Java, variable names should start with a lowercase letter always. Also `List B = A;` does probably something else than what you think it does. – Turing85 Dec 30 '17 at 19:24
  • You have some really confusing variable names - XY is just X (consider instead using xs or sx - s for String), YX is just Y, QW is XY (because concat of X and Y) and WE is YX. – Bernhard Barker Dec 30 '17 at 19:29
  • 1
    [How to convert a String to an int in Java?](https://stackoverflow.com/q/5585779) – Bernhard Barker Dec 30 '17 at 19:35
  • 1
    Please read [mcve] and enhance your question accordingly. – GhostCat Dec 30 '17 at 19:59

3 Answers3

0

Are you trying to sort in alphabetical order? If so, I'd imagine something like

public int compare(Integer X, Integer Y) {
    return X.toString().compareTo(Y.toString());
}

would do the trick.

Otherwise, I'd drop this from the Collections.sort() line and it should sort it in numerical order.

nachtm
  • 26
  • 3
0

Let us have a look at your code.

  • The input array consists of 00000. I guess this means an array of five 0s: [0,0,0,0,0].
  • You are sorting the input via the compare method. In this case sorting has no effect as we have only 0s in.
  • The forloop is executed

    for (int i=B.size()-1; i >= 0; i--) {
      str = str.append(B.get(i));
    }
    

    You are iterating through the array and append each element of the array to the string builder str. So you append 0, 0, 0, 0, 0 which gives as string 00000.

This should explain the seen behaviour. You could e.g. try convert the string 00000 to an int and then return this.

Stefan Freitag
  • 3,578
  • 3
  • 26
  • 33
0

You concatenate strings and this is qiute obvious you may have some leading, isignificant zeroes at the beginning. This is nothing unexpected. To deal with them, iterate using StringBuilder's method at() to find first non-zero character and then return the result of substring() method. Just remember to take the character sequenece length into account too, so in case of all zeroes get the last one indexed when calling substring(). This way, depending on sorting direction you'd produce eithr largest or lowest number from the list.

Tomator
  • 81
  • 8