h_多个0问题

来源:互联网 发布:大数据与精准营销 编辑:程序博客网 时间:2024/05/22 13:39

。。记下出现0的次数就行了吧。。。


正解,只需一次遍历,判断为0,直接拿后面第一个非0的数填充,非0数填充为0,遍历完了 0都移到最后了

Java代码 复制代码
  1. package com.gpdi.strcom;   
  2.   
  3. import com.gpdi.sort.*;   
  4.   
  5. /**  
  6.  * @author Administrator  
  7.  *  
  8.  */  
  9. public class Test01 {   
  10.        
  11.     private static  int [] DATA = {0,1,0,0,3,4,5,0,0,0,0,10,99,32754,0,0};   
  12.     /**  
  13.      * @param args  
  14.      */  
  15.     public static void main(String[] args) {   
  16.         // TODO Auto-generated method stub   
  17.         moveZero(DATA,DATA.length);   
  18.         for (int var : DATA  ){   
  19.             System.out.println(var);   
  20.         }   
  21.     }   
  22.        
  23.        
  24.     private static void moveZero(int [] data,int size){   
  25.         for (int i =0 ; i<size; i++){   
  26.             while0 != data[i])   
  27.             {   
  28.                 i++;   
  29.             }   
  30.             int j = i;   
  31.             while ((j<size)&&(0==data[j])) {   
  32.                 j++;   
  33.             }   
  34.             if ((j>=i)&&(i<size)&&(j<size)) {   
  35.                 SortUtil.sortSwap(data, i, j);   
  36.             }   
  37.         }   
  38.     }   
  39. }  

 

 

不知道这个有序是啥意思
如果是保持原来的顺序的话,那就按照下面的来
如果是大小有序的话,那就先一遍两头遍历,把0放后面;然后再把前面非0的部分排个序。

C++代码 复制代码
  1. int *p1, *p2;   
  2. p1 = p2 = A;   
  3. // 初始状态是p1和p2从第一个元素开始,p1移动到第一个0元素,p2移动到p1后第一个非0的元素   
  4. // 中间状态是p1指向第一个是0的元素,p2指向p1后第一个非零元素   
  5. // 比如 ....000000...02304.....   
  6. //          p1        p2   
  7. // 然后交换一下   
  8. // 结束时p2在数组末尾   
  9. // ......23000000.....000   
  10. //         p1           p2   
  11. // 这样一次遍历就够了   
  12. // 移动p1到第一个0值   
  13. while(p1 < A + nSize && *p1 != 0) p1++;   
  14. if(p1 == A + nSize) return;   
  15. p2 = p1;   
  16. while(p2 < A + nSize)   
  17. {   
  18.     // 移动p1到第一个0值   
  19.     while(p1 < A + nSize && *p1 != 0) p1++;   
  20.     if(p1 == A + nSize) return;   
  21.     // 移动p2到p1后的第一个非0值   
  22.     while(p2 < A + nSize && *p2 == 0) p2++;   
  23.     if(p2 == A + nSize) return;   
  24.     // 交换一下位置   
  25.     *p1 = *p2;   
  26.     *p2 = 0;   
  27. }  

自己尝试做了下,以下是代码:

C++代码 复制代码
  1. #include <iostream>   
  2. using namespace std;   
  3. /*  
  4.  * int A[nSize],其中隐藏着若干0,其余非0整数,写一个函数int Func(int* A, int nSize),使A把0移至后面,非0整数移至  
  5.  * 数组前面并保持有序,返回值为原数据中第一个元素为0的下标。(尽可能不使用辅助空间且考虑效率及异常问题,注释规范且给出设计思路)  
  6.  *  
  7.  */  
  8. int Func(int *A, int nSize) {   
  9.     int index = (nSize - 1);   
  10.     //由后往前   
  11.     while ((nSize--) >= 0) {   
  12.         if (*(A + nSize) == 0) {//为零,直接下标前移   
  13.             index = nSize;   
  14.             continue;   
  15.         }   
  16.         //否则拿前面的数据与自己相比较   
  17.         for (int i = 0; i < nSize; i++) {   
  18.             int last = *(A + nSize);   
  19.             int pre = *(A + i);   
  20.             if (pre == 0) {//直接交换,跳出for循环   
  21.                 *(A + nSize) = pre;   
  22.                 *(A + i) = last;   
  23.                 index = nSize;   
  24.                 break;   
  25.             }   
  26.             if (last > pre) {//排序交换   
  27.                 *(A + nSize) = pre;   
  28.                 *(A + i) = last;   
  29.                 ;//swap   
  30.             }   
  31.         }   
  32.     }   
  33.     return index;   
  34. }   
  35.   
  36. int main(void) {   
  37.     const int intSize = 10;   
  38.     int A[intSize] = { 0, 4, 7, 2, 160, 0, 34, 21, 0, 19, 107 };   
  39.     cout << "The return value= " << Func(&A[0], intSize);   
  40.     return 0;   
  41. }  
#include <iostream>using namespace std;/* * int A[nSize],其中隐藏着若干0,其余非0整数,写一个函数int Func(int* A, int nSize),使A把0移至后面,非0整数移至 * 数组前面并保持有序,返回值为原数据中第一个元素为0的下标。(尽可能不使用辅助空间且考虑效率及异常问题,注释规范且给出设计思路) * */int Func(int *A, int nSize) {int index = (nSize - 1);//由后往前while ((nSize--) >= 0) {if (*(A + nSize) == 0) {//为零,直接下标前移index = nSize;continue;}//否则拿前面的数据与自己相比较for (int i = 0; i < nSize; i++) {int last = *(A + nSize);int pre = *(A + i);if (pre == 0) {//直接交换,跳出for循环*(A + nSize) = pre;*(A + i) = last;index = nSize;break;}if (last > pre) {//排序交换*(A + nSize) = pre;*(A + i) = last;;//swap}}}return index;}int main(void) {const int intSize = 10;int A[intSize] = { 0, 4, 7, 2, 160, 0, 34, 21, 0, 19, 107 };cout << "The return value= " << Func(&A[0], intSize);return 0;}

优秀方案:
由第二页Turbo 编写
一个循环就搞掂了.
Java代码 复制代码
  1. public static void main(String[] args) {      
  2.     int[] a = { 047216003421019107 };      
  3.     int j = 0;      
  4.     for (int i = 0; i < a.length; i++) {      
  5.         if (a[i] == 0 && i != a.length - 1 && a[i + 1] != 0) {      
  6.             a[j++] = a[i + 1];      
  7.             a[i + 1] = 0;      
  8.         }      
  9.     }      
  10.      
  11.     System.out.println(Arrays.toString(a));      
  12. }    

原创粉丝点击