java字串替換排序執行個體_java

來源:互聯網
上載者:User

複製代碼 代碼如下:

import java.util.LinkedList;

public class OJ {

 public OJ() {
  super();
 }

 /*
  * 功能:輸入一行數字,如果我們把這行數字中的'5'都看成空格,那麼就得到一行用空格分隔的非負整數(可能有些整數以'0'開頭,這些頭部的'0'應該被忽略掉
  * ,除非這個整數就是由若干個'0'組成的,這時這個整數就是0)。 對這些非負整數按從大到小的順序排序。
  *
  * 輸入: input,由0~9數字組成的字串。
  * 輸入資料要求:無非數字字元,長度大於0且不大於1000,分隔得到的非負整數不會大於100000000,輸入資料不可能全由'5'組成。
  *
  * 輸出:output,字串,是分隔得到的非負整數排序結果(從大到小),相鄰整數間用一個空格分開,第一個整數前及最後一個整數後無空格。
  *
  * 返回:正常返回0,若異常失敗返回-1。
  */

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

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

  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;
     }
     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;
 }
}       

測試案例:

複製代碼 代碼如下:

import junit.framework.Assert;
import junit.framework.TestCase;


public class OJTest extends TestCase
{

    public void testCase01()
    {
        // 這裡寫測試案例
        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 67".equals(output.toString()));
    }

    public void testCase04()
    {
        final StringBuilder output = new StringBuilder();
        Assert.assertTrue(0 == OJ.getSortNum("123454321555000000000056436567", output) && "6436 4321 1234 67 0".equals(output.toString()));
    }

    public void testCase05()
    {
        final StringBuilder output = new StringBuilder();
        Assert.assertTrue(-1 == OJ.getSortNum("1234543215510000000000000001556436567", output));
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.