Здравейте. Моля за малко съдействие с тази задача:
Given an array integers, write a program that moves all of the zeroes to the end of it, while maintaining the relative order of the non-zero elements.
Input
Read from the standard input:
- There is one line of input, containing N amount of integers, seperated by a comma (",")
Output
Print to the standard output:
- There is one line of outpit, containing the sorted integers, seperated by a comma (",")
Дава ми 80 % и послените 2 теста не минават. Решила съм я по този начин и се опитвам да открия къде греша. Това е моят код:
import java.util.Arrays;
import java.util.Scanner;
public class ArraySort {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
String[] numbersAsString = input.split(",");
int[] nums = new int[numbersAsString.length];
int[] otherNums = new int[nums.length];
int position = 0;
StringBuilder positiveNumbers = new StringBuilder();
StringBuilder zeros = new StringBuilder();
for (int i = 0; i < nums.length; i++) {
nums[i] = Integer.parseInt(numbersAsString[i]);
if (nums[i] > 0) {
otherNums[position] = nums[i];
positiveNumbers.append(nums[i]).append(",");
position++;
}
}
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 0) {
otherNums[position] = nums[i];
zeros.append(nums[i]).append(",");
position++;
}
}
if (zeros.charAt(zeros.length() - 1) == ',') {
zeros.deleteCharAt(zeros.length() - 1);}
}
System.out.print(positiveNumbers);
System.out.print(zeros);
}
}
Благодаря предварително.