Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 创建基本压缩算法_Java_Algorithm_Compression - Fatal编程技术网

Java 创建基本压缩算法

Java 创建基本压缩算法,java,algorithm,compression,Java,Algorithm,Compression,主要方法 :获取整数数组并使用SparseCompression()将其压缩为字符串 压缩算法: 到目前为止,数组必须以连续的零开始,然后以第一个非零结束。需要能够从非零开始,并一直持续到阵列结束。类似于(4,0,0,0,0,45,65,0,0,0,0,5) 公共静态字符串SparseCompression(int[]数组) { 整数计数=0; int非零=0; int后=0; for(int i=0;i

主要方法 :获取整数数组并使用SparseCompression()将其压缩为字符串

压缩算法: 到目前为止,数组必须以连续的零开始,然后以第一个非零结束。需要能够从非零开始,并一直持续到阵列结束。类似于(4,0,0,0,0,45,65,0,0,0,0,5)

公共静态字符串SparseCompression(int[]数组)
{  
整数计数=0;
int非零=0;
int后=0;
for(int i=0;i
您的问题是什么?看起来这是一个家庭作业问题,你不明白你被要求做什么。请仔细阅读关于家庭作业问题的网站政策。
public class Compress
{
   public static void main(String[] args)
   {
      int[] test = {0, 0, 0, 0, 0, 0, 0, 999};
      String result = SparseCompression(test);

      // expected result: "#7,999," 
      System.out.println(result);
      //need to compress something like(2,0,0,0,54,0,0,2)
      // expected result: "2,#3,54,#2,2"
   }
public static String SparseCompression(int[] array)
   {  
      int count = 0;
      int nonZero = 0;
      int after = 0;
      for(int i = 0; i< array.length; i++)
      {
         if(array[i] == 0)
         {
            count++;
         }
         else if(array[i] != 0)
         {
           nonZero = array[i];
         }

      }
      Integer.toString(count);
      String result = "#" + count + ", " + nonZero;
      return result;
      }
   }
public static String SparseCompression(int[] array) {
    if (null == array || array.length == 0) {
        return "";
    }
    StringBuilder sb = new StringBuilder();
    int count = 0;
    for (int a : array) {
        if (a == 0) {
            count++;
            continue;
        }
        if (count != 0) {
            sb.append("#" + count + ",");
            count = 0;
        }
        sb.append(a + ",");
    }
    if (count != 0) {
        sb.append("#" + count + ",");
    }
    return sb.toString().substring(0, sb.length() - 1);
}