Java string Replacement Sort instance _java

Source: Internet
Author: User

Copy Code code as follows:

Import java.util.LinkedList;

public class OJ {

Public OJ () {
Super ();
}

/*
* Function: Enter a line of numbers, if we think of the ' 5 ' in this line as a space, then we get a line with a space-separated nonnegative integer (maybe some integers start with ' 0 ', and the ' 0 ' of these heads should be ignored.
* Unless the integer is made up of a number of ' 0 ', then the integer is 0. The non-negative integers are sorted in order from large to small.
*
* Input: Input, a string consisting of 0~9 digits.
* Input data requirements: No more than the number of characters, length greater than 0 and not greater than 1000, separated by the nonnegative integer will not be greater than 100000000, the input data can not be all composed of ' 5 '.
*
* Outputs: Output, string, is separated by the result of nonnegative integer ordering (from large to small), adjacent integers separated by a space, the first integer before and after the last integer no space.
*
* Return: normal return 0, if abnormal failure to return-1.
*/

 public static int getsortnum (final String input, final StringBuilder output) {

  if ( Input.length () <=0 | | Input.length () > 1000) {
   return-1;
&NBSP;&NBSP}

  string[] nums = Input.split ("5");
  int len = nums.length;
  LinkedList<Integer> sorted = new linkedlist<integer> ();

  int j = 0;
  for (int i = 0; i < len; i++) {
   int temp = 0;
   int k = j;
   if (!nums[i].equals (")) {
    try {
      temp = integer.valueof (Nums[i]);
     if (Temp > 100000000) {
      return-1
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}
     if (Sorted.isempty ()) {
       sorted.addfirst (temp);
      j++;
     } else {

while (k > 0 && (Temp > Sorted.get (k-1)) {
k--;
}
Sorted.add (k, temp);
j + +;
}
catch (Exception ex) {
return-1;
}
}
}
for (int i = 0; i < sorted.size ()-1; i++) {

Output.append (Sorted.get (i) + "");

}
Output.append (Sorted.getlast ());
Output.trimtosize ();

return 0;
}
}

Test Cases:

Copy Code code as follows:

Import Junit.framework.Assert;
Import Junit.framework.TestCase;


public class Ojtest extends TestCase
{

public void TestCase01 ()
{
Write Test Cases here
Final StringBuilder output = new StringBuilder ();
Assert.asserttrue ( -1 = Oj.getsortnum ("1234543 215555", output));
}

   
    public void TestCase02 ()
    {
         final StringBuilder output = new StringBuilder ();
        assert.asserttrue (0 = oj.getsortnum ("1234543215555", Output) & & "4321 1234". Equals (Output.tostring ()));
   }

    public void testCase03 ()
    {
         final StringBuilder output = new StringBuilder ();
        assert.asserttrue (0 = oj.getsortnum ("12345432155556436567", output ) && "6436 4321 1234". Equals (Output.tostring ());
   }

public void TestCase04 ()
{
Final StringBuilder output = new StringBuilder ();
Assert.asserttrue (0 = oj.getsortnum ("123454321555000000000056436567", Output) && "6436 4321 1234 0". Equals ( Output.tostring ()));
}

public void TestCase05 ()
{
Final StringBuilder output = new StringBuilder ();
Assert.asserttrue ( -1 = Oj.getsortnum ("1234543215510000000000000001556436567", output));
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.