Optimizing Array Compaction

前端 未结 5 938
耶瑟儿~
耶瑟儿~ 2021-02-07 01:52

Let\'s say I have an array k = [1 2 0 0 5 4 0]

I can compute a mask as follows m = k > 0 = [1 1 0 0 1 1 0]

Using only the mask m and

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-07 02:16

    Assuming what you want is to store only positive integers from an array with minimum steps in C++ this is a sample code:

    int j = 0;
    int arraysize = (sizeof k)/4;
    int store[arraysize];
    for(int i = 0; i 0)
        {
            store[j] = k[i];
            j++;
        }
    }
    

    Or you can directly use elements of k[ ] if you don't want to use for loop.

提交回复
热议问题